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