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