View Javadoc

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.setProject( getProject() );
93          depsTask.setPathId( "maven-core-dependencies" );
94          depsTask.addDependency( mavenCore );
95          
96          depsTask.execute();
97          
98          this.setClasspath( (Path) getProject().getReference( "maven-core-dependencies" ) );
99          
100         this.setClassname( "org.apache.maven.cli.MavenCli" );
101     }
102     
103     private void setupLocalMaven()
104     {
105         // Set the required properties
106         Variable classworldsConfProp = new Variable();
107         classworldsConfProp.setKey( "classworlds.conf" );
108         File classworldsPath = new File( mavenHome, "bin/m2.conf" );
109         classworldsConfProp.setValue( classworldsPath.getAbsolutePath() );
110         this.addSysproperty( classworldsConfProp );
111         
112         Variable mavenHomeProp = new Variable();
113         mavenHomeProp.setKey( "maven.home" );
114         mavenHomeProp.setValue( mavenHome.getAbsolutePath() );
115         this.addSysproperty( mavenHomeProp );
116         
117         // Set the boot classpath
118         FileSet bootDir = new FileSet();
119         bootDir.setDir( new File ( mavenHome, "boot" ) );
120         bootDir.setIncludes( "*.jar" );
121         
122         Path mavenClasspath = new Path( getProject() );
123         mavenClasspath.addFileset( bootDir );
124         
125         this.setClasspath( mavenClasspath );
126         
127         this.setClassname( "org.codehaus.classworlds.Launcher" );
128     }
129 
130     public void setPom( File pom )
131     {
132         this.pom = pom;
133     }
134 
135     public File getPom()
136     {
137         return pom;
138     }
139 
140     public void setMavenHome( File mavenHome )
141     {
142         this.mavenHome = mavenHome;
143     }
144 
145     public File getMavenHome()
146     {
147         return mavenHome;
148     }
149 
150     public void setBatchMode( boolean batchMode )
151     {
152         this.batchMode = batchMode;
153     }
154 
155     public boolean isBatchMode()
156     {
157         return batchMode;
158     }
159 
160     public void addLocalRepository( LocalRepository localRepository )
161     {
162         this.localRepository = localRepository;
163     }
164 
165     public LocalRepository getLocalRepository()
166     {
167         return localRepository;
168     }
169 
170     public void setMavenVersion( String mavenVersion )
171     {
172         this.mavenVersion = mavenVersion;
173     }
174 
175     public String getMavenVersion()
176     {
177         return mavenVersion;
178     }
179 
180 }