Save This Page
Home » maven-ant-tasks-2.1.0-src » org.apache » maven » artifact » ant » [javadoc | source]
    1   package org.apache.maven.artifact.ant;
    2   
    3   /*
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
   10    * with the License.  You may obtain a copy of the License at
   11    *
   12    *  http://www.apache.org/licenses/LICENSE-2.0
   13    *
   14    * Unless required by applicable law or agreed to in writing,
   15    * software distributed under the License is distributed on an
   16    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   17    * KIND, either express or implied.  See the License for the
   18    * specific language governing permissions and limitations
   19    * under the License.
   20    */
   21   
   22   import java.io.File;
   23   
   24   import org.apache.maven.model.Dependency;
   25   import org.apache.tools.ant.BuildException;
   26   import org.apache.tools.ant.taskdefs.Java;
   27   import org.apache.tools.ant.types.FileSet;
   28   import org.apache.tools.ant.types.Path;
   29   import org.apache.tools.ant.types.Environment.Variable;
   30   
   31   /**
   32    * Ant task to execute a maven build.
   33    * 
   34    * @author pgier
   35    *
   36    */
   37   public class Mvn
   38       extends Java
   39   {
   40   
   41       public final String BATCH_MODE = "-B";
   42       
   43       private File pom;
   44       
   45       private File mavenHome;
   46       
   47       private final String DEFAULT_MAVEN_VERSION = "2.0.10";
   48       
   49       private String mavenVersion = DEFAULT_MAVEN_VERSION;
   50       
   51       private boolean batchMode = true;
   52       
   53       private LocalRepository localRepository;
   54       
   55       public void execute() throws BuildException
   56       {
   57           if ( batchMode )
   58           {
   59               this.createArg().setValue( BATCH_MODE );
   60           }
   61           
   62           if ( pom != null )
   63           {
   64               createArg().setValue( "-f" + pom.getAbsolutePath() );
   65           }
   66           
   67           if ( localRepository != null )
   68           {
   69               this.createJvmarg().setValue( "-Dmaven.repo.local=" + localRepository.getPath().getAbsolutePath() );
   70           }
   71           
   72           if ( mavenHome == null )
   73           {
   74               downloadAndConfigureMaven();
   75           }
   76           else
   77           {
   78               setupLocalMaven();
   79           }
   80           
   81           super.execute();
   82       }
   83       
   84       private void downloadAndConfigureMaven()
   85       {
   86           Dependency mavenCore = new Dependency();
   87           mavenCore.setGroupId( "org.apache.maven" );
   88           mavenCore.setArtifactId( "maven-core" );
   89           mavenCore.setVersion( getMavenVersion() );
   90           
   91           DependenciesTask depsTask = new DependenciesTask();
   92           depsTask.addLocalRepository( getLocalRepository() );
   93           depsTask.setProject( getProject() );
   94           depsTask.setPathId( "maven-core-dependencies" );
   95           depsTask.addDependency( mavenCore );
   96           
   97           depsTask.execute();
   98           
   99           this.setClasspath( (Path) getProject().getReference( "maven-core-dependencies" ) );
  100           
  101           this.setClassname( "org.apache.maven.cli.MavenCli" );
  102       }
  103       
  104       private void setupLocalMaven()
  105       {
  106           // Set the required properties
  107           Variable classworldsConfProp = new Variable();
  108           classworldsConfProp.setKey( "classworlds.conf" );
  109           File classworldsPath = new File( mavenHome, "bin/m2.conf" );
  110           classworldsConfProp.setValue( classworldsPath.getAbsolutePath() );
  111           this.addSysproperty( classworldsConfProp );
  112           
  113           Variable mavenHomeProp = new Variable();
  114           mavenHomeProp.setKey( "maven.home" );
  115           mavenHomeProp.setValue( mavenHome.getAbsolutePath() );
  116           this.addSysproperty( mavenHomeProp );
  117           
  118           // Set the boot classpath
  119           FileSet bootDir = new FileSet();
  120           bootDir.setDir( new File ( mavenHome, "boot" ) );
  121           bootDir.setIncludes( "*.jar" );
  122           
  123           Path mavenClasspath = new Path( getProject() );
  124           mavenClasspath.addFileset( bootDir );
  125           
  126           this.setClasspath( mavenClasspath );
  127           
  128           this.setClassname( "org.codehaus.classworlds.Launcher" );
  129       }
  130   
  131       public void setPom( File pom )
  132       {
  133           this.pom = pom;
  134       }
  135   
  136       public File getPom()
  137       {
  138           return pom;
  139       }
  140   
  141       public void setMavenHome( File mavenHome )
  142       {
  143           this.mavenHome = mavenHome;
  144       }
  145   
  146       public File getMavenHome()
  147       {
  148           return mavenHome;
  149       }
  150   
  151       public void setBatchMode( boolean batchMode )
  152       {
  153           this.batchMode = batchMode;
  154       }
  155   
  156       public boolean isBatchMode()
  157       {
  158           return batchMode;
  159       }
  160   
  161       public void addLocalRepository( LocalRepository localRepository )
  162       {
  163           this.localRepository = localRepository;
  164       }
  165   
  166       public LocalRepository getLocalRepository()
  167       {
  168           return localRepository;
  169       }
  170   
  171       public void setMavenVersion( String mavenVersion )
  172       {
  173           this.mavenVersion = mavenVersion;
  174       }
  175   
  176       public String getMavenVersion()
  177       {
  178           return mavenVersion;
  179       }
  180   
  181   }

Save This Page
Home » maven-ant-tasks-2.1.0-src » org.apache » maven » artifact » ant » [javadoc | source]