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  import java.util.regex.Pattern;
24  
25  import org.apache.maven.model.Dependency;
26  import org.apache.tools.ant.BuildException;
27  import org.apache.tools.ant.taskdefs.Java;
28  import org.apache.tools.ant.types.FileSet;
29  import org.apache.tools.ant.types.Path;
30  import org.apache.tools.ant.types.Environment.Variable;
31  
32  /**
33   * Ant task to execute a maven build.
34   * 
35   * @author pgier
36   *
37   */
38  public class Mvn
39      extends Java
40  {
41  
42      public final String BATCH_MODE = "-B";
43      
44      private File pom;
45      
46      private File mavenHome;
47      
48      private final String DEFAULT_MAVEN_VERSION = "2.0.10";
49      
50      private String mavenVersion = DEFAULT_MAVEN_VERSION;
51      
52      private boolean batchMode = true;
53      
54      private LocalRepository localRepository;
55      
56      public void execute()
57          throws BuildException
58      {
59          if ( batchMode )
60          {
61              this.createArg().setValue( BATCH_MODE );
62          }
63          
64          if ( pom != null )
65          {
66              createArg().setValue( "-f" + pom.getAbsolutePath() );
67          }
68          
69          if ( localRepository != null )
70          {
71              this.createJvmarg().setValue( "-Dmaven.repo.local=" + localRepository.getPath().getAbsolutePath() );
72          }
73          
74          if ( mavenHome == null )
75          {
76              Pattern oldMaven = Pattern.compile("(2\\.0)|(2\\.0-.*)|(2\\.0\\.[1-9])");
77              if ( oldMaven.matcher( getMavenVersion() ).matches() )
78              {
79                  throw new BuildException( "The requested Maven version '" + getMavenVersion() + "' is prior to " +
80                                            "version '2.0.10'. In order to launch the requested version you need to " +
81                                            "use a local Maven installation and point to that installation with the " +
82                                            "mavenHome attribute." );
83              }
84              downloadAndConfigureMaven();
85          }
86          else
87          {
88              setupLocalMaven();
89          }
90          
91          super.execute();
92      }
93      
94      private void downloadAndConfigureMaven()
95      {
96          Dependency apacheMaven = new Dependency();
97          apacheMaven.setGroupId( "org.apache.maven" );
98          apacheMaven.setArtifactId( "apache-maven" );
99          apacheMaven.setVersion( getMavenVersion() );
100         apacheMaven.setType( "pom" );
101         
102         DependenciesTask depsTask = new DependenciesTask();
103         depsTask.addLocalRepository( getLocalRepository() );
104         depsTask.setProject( getProject() );
105         depsTask.setPathId( "apache-maven-dependencies" );
106         depsTask.addDependency( apacheMaven );
107         depsTask.setType( "pom,jar" );
108         depsTask.setPathType( "jar" );
109 
110         depsTask.execute();
111         
112         this.setClasspath( (Path) getProject().getReference( "apache-maven-dependencies" ) );
113         
114         this.setClassname( "org.apache.maven.cli.MavenCli" );
115     }
116     
117     private void setupLocalMaven()
118     {
119         // Set the required properties
120         Variable classworldsConfProp = new Variable();
121         classworldsConfProp.setKey( "classworlds.conf" );
122         File classworldsPath = new File( mavenHome, "bin/m2.conf" );
123         classworldsConfProp.setValue( classworldsPath.getAbsolutePath() );
124         this.addSysproperty( classworldsConfProp );
125         
126         Variable mavenHomeProp = new Variable();
127         mavenHomeProp.setKey( "maven.home" );
128         mavenHomeProp.setValue( mavenHome.getAbsolutePath() );
129         this.addSysproperty( mavenHomeProp );
130         
131         // Set the boot classpath
132         FileSet bootDir = new FileSet();
133         bootDir.setDir( new File ( mavenHome, "boot" ) );
134         bootDir.setIncludes( "*.jar" );
135         
136         Path mavenClasspath = new Path( getProject() );
137         mavenClasspath.addFileset( bootDir );
138         
139         this.setClasspath( mavenClasspath );
140         
141         this.setClassname( "org.codehaus.classworlds.Launcher" );
142     }
143 
144     public void setPom( File pom )
145     {
146         this.pom = pom;
147     }
148 
149     public File getPom()
150     {
151         return pom;
152     }
153 
154     public void setMavenHome( File mavenHome )
155     {
156         this.mavenHome = mavenHome;
157     }
158 
159     public File getMavenHome()
160     {
161         return mavenHome;
162     }
163 
164     public void setBatchMode( boolean batchMode )
165     {
166         this.batchMode = batchMode;
167     }
168 
169     public boolean isBatchMode()
170     {
171         return batchMode;
172     }
173 
174     public void addLocalRepository( LocalRepository localRepository )
175     {
176         this.localRepository = localRepository;
177     }
178 
179     public LocalRepository getLocalRepository()
180     {
181         return localRepository;
182     }
183 
184     public void setMavenVersion( String mavenVersion )
185     {
186         this.mavenVersion = mavenVersion;
187     }
188 
189     public String getMavenVersion()
190     {
191         return mavenVersion;
192     }
193 
194 }