Author: pgier Date: Wed May 6 22:53:23 2009 New Revision: 772461 URL: http://svn.apache.org/viewvc?rev=772461&view=rev Log: [MANTTASKS-71] Add mvn task that can be used to call a maven build. Also add documentation for using the java task to call maven.
Added: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/java/org/apache/maven/artifact/ant/Mvn.java (with props) maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/examples/mvn.apt (with props) maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/test/pom.xml (with props) Modified: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/sample.build.xml maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/resources/org/apache/maven/artifact/ant/antlib.xml maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/examples/pom.apt maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/index.apt maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/site.xml Modified: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/sample.build.xml URL: http://svn.apache.org/viewvc/maven/ant-tasks/branches/maven-ant-tasks-2.0.x/sample.build.xml?rev=772461&r1=772460&r2=772461&view=diff ============================================================================== --- maven/ant-tasks/branches/maven-ant-tasks-2.0.x/sample.build.xml (original) +++ maven/ant-tasks/branches/maven-ant-tasks-2.0.x/sample.build.xml Wed May 6 22:53:23 2009 @@ -71,7 +71,7 @@ depends="test-deploy-spaces,test-deploy,test-no-dist-mgmt,test-install-attached,test-deploy-attached"/> <target name="test-all" description="All tests" - depends="test-all-deps,test-all-pubs,test-install-provider,checkClassloader"> + depends="test-all-deps,test-all-pubs,test-install-provider,checkClassloader,test-mvn"> <echo>Tests completed successfully.</echo> <echo>test-scm not run, since it fails...</echo> </target> @@ -576,6 +576,13 @@ </artifact:pom> <check.file.exists file="${basedir}/target/tmp/it/ant-tasks/pom/1.1/pom-1.1.pom" type="file"/> </target> + + <target name="test-mvn" depends="initTaskDefs"> + <artifact:mvn pom="src/test/pom.xml" mavenVersion="2.1.0" fork="true"> + <arg value="package"/> + <localRepository path="${basedir}/target/tmp"/> + </artifact:mvn> + </target> <macrodef name="check.file.exists"> <attribute name="file"/> Added: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/java/org/apache/maven/artifact/ant/Mvn.java URL: http://svn.apache.org/viewvc/maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/java/org/apache/maven/artifact/ant/Mvn.java?rev=772461&view=auto ============================================================================== --- maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/java/org/apache/maven/artifact/ant/Mvn.java (added) +++ maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/java/org/apache/maven/artifact/ant/Mvn.java Wed May 6 22:53:23 2009 @@ -0,0 +1,180 @@ +package org.apache.maven.artifact.ant; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; + +import org.apache.maven.model.Dependency; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.taskdefs.Java; +import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.types.Environment.Variable; + +/** + * Ant task to execute a maven build. + * + * @author pgier + * + */ +public class Mvn + extends Java +{ + + public final String BATCH_MODE = "-B"; + + private File pom; + + private File mavenHome; + + private final String DEFAULT_MAVEN_VERSION = "2.0.10"; + + private String mavenVersion = DEFAULT_MAVEN_VERSION; + + private boolean batchMode = true; + + private LocalRepository localRepository; + + public void execute() throws BuildException + { + if ( batchMode ) + { + this.createArg().setValue( BATCH_MODE ); + } + + if ( pom != null ) + { + createArg().setValue( "-f" + pom.getAbsolutePath() ); + } + + if ( localRepository != null ) + { + this.createJvmarg().setValue( "-Dmaven.repo.local=" + localRepository.getPath().getAbsolutePath() ); + } + + if ( mavenHome == null ) + { + downloadAndConfigureMaven(); + } + else + { + setupLocalMaven(); + } + + super.execute(); + } + + private void downloadAndConfigureMaven() + { + Dependency mavenCore = new Dependency(); + mavenCore.setGroupId( "org.apache.maven" ); + mavenCore.setArtifactId( "maven-core" ); + mavenCore.setVersion( getMavenVersion() ); + + DependenciesTask depsTask = new DependenciesTask(); + depsTask.setProject( getProject() ); + depsTask.setPathId( "maven-core-dependencies" ); + depsTask.addDependency( mavenCore ); + + depsTask.execute(); + + this.setClasspath( (Path) getProject().getReference( "maven-core-dependencies" ) ); + + this.setClassname( "org.apache.maven.cli.MavenCli" ); + } + + private void setupLocalMaven() + { + // Set the required properties + Variable classworldsConfProp = new Variable(); + classworldsConfProp.setKey( "classworlds.conf" ); + File classworldsPath = new File( mavenHome, "bin/m2.conf" ); + classworldsConfProp.setValue( classworldsPath.getAbsolutePath() ); + this.addSysproperty( classworldsConfProp ); + + Variable mavenHomeProp = new Variable(); + mavenHomeProp.setKey( "maven.home" ); + mavenHomeProp.setValue( mavenHome.getAbsolutePath() ); + this.addSysproperty( mavenHomeProp ); + + // Set the boot classpath + FileSet bootDir = new FileSet(); + bootDir.setDir( new File ( mavenHome, "boot" ) ); + bootDir.setIncludes( "*.jar" ); + + Path mavenClasspath = new Path( getProject() ); + mavenClasspath.addFileset( bootDir ); + + this.setClasspath( mavenClasspath ); + + this.setClassname( "org.codehaus.classworlds.Launcher" ); + } + + public void setPom( File pom ) + { + this.pom = pom; + } + + public File getPom() + { + return pom; + } + + public void setMavenHome( File mavenHome ) + { + this.mavenHome = mavenHome; + } + + public File getMavenHome() + { + return mavenHome; + } + + public void setBatchMode( boolean batchMode ) + { + this.batchMode = batchMode; + } + + public boolean isBatchMode() + { + return batchMode; + } + + public void addLocalRepository( LocalRepository localRepository ) + { + this.localRepository = localRepository; + } + + public LocalRepository getLocalRepository() + { + return localRepository; + } + + public void setMavenVersion( String mavenVersion ) + { + this.mavenVersion = mavenVersion; + } + + public String getMavenVersion() + { + return mavenVersion; + } + +} Propchange: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/java/org/apache/maven/artifact/ant/Mvn.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/java/org/apache/maven/artifact/ant/Mvn.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Modified: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/resources/org/apache/maven/artifact/ant/antlib.xml URL: http://svn.apache.org/viewvc/maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/resources/org/apache/maven/artifact/ant/antlib.xml?rev=772461&r1=772460&r2=772461&view=diff ============================================================================== --- maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/resources/org/apache/maven/artifact/ant/antlib.xml (original) +++ maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/main/resources/org/apache/maven/artifact/ant/antlib.xml Wed May 6 22:53:23 2009 @@ -5,6 +5,7 @@ <taskdef name="install" classname="org.apache.maven.artifact.ant.InstallTask"/> <taskdef name="deploy" classname="org.apache.maven.artifact.ant.DeployTask"/> <taskdef name="install-provider" classname="org.apache.maven.artifact.ant.InstallWagonProviderTask"/> + <taskdef name="mvn" classname="org.apache.maven.artifact.ant.Mvn"/> <!-- Types --> <typedef name="localRepository" classname="org.apache.maven.artifact.ant.LocalRepository"/> Added: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/examples/mvn.apt URL: http://svn.apache.org/viewvc/maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/examples/mvn.apt?rev=772461&view=auto ============================================================================== --- maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/examples/mvn.apt (added) +++ maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/examples/mvn.apt Wed May 6 22:53:23 2009 @@ -0,0 +1,117 @@ + ------ + Mvn + ------ + Paul Gier + ------ + 2009-05-06 + ------ + + ~~ Licensed to the Apache Software Foundation (ASF) under one + ~~ or more contributor license agreements. See the NOTICE file + ~~ distributed with this work for additional information + ~~ regarding copyright ownership. The ASF licenses this file + ~~ to you under the Apache License, Version 2.0 (the + ~~ "License"); you may not use this file except in compliance + ~~ with the License. You may obtain a copy of the License at + ~~ + ~~ http://www.apache.org/licenses/LICENSE-2.0 + ~~ + ~~ Unless required by applicable law or agreed to in writing, + ~~ software distributed under the License is distributed on an + ~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~~ KIND, either express or implied. See the License for the + ~~ specific language governing permissions and limitations + ~~ under the License. + + ~~ NOTE: For help with the syntax of this file, see: + ~~ http://maven.apache.org/doxia/references/apt-format.html + +The Mvn Task + + <<Note: This task is only available since version 2.0.10>> + + The maven ant tasks have some limited support for calling a full maven build from ant. + The <<<mvn>>> task is a subclass of the Ant {{{java}http://ant.apache.org/manual/CoreTasks/java.html}} + task and supports all of it's options such as args, fork, resultproperty, etc. + + If maven is already installed on the local system the maven build can be called using + this local installation by specifying the <<<mavenHome>>> parameter. + +----- + <artifact:mvn mavenHome="/path/to/maven-2.0.x"> + <arg value="install"/> + </artifact:mvn> +----- + + Maven will search for a pom.xml file in the current directory and run the <<<install>>> goal. + + If the pom file is not located in the current directory, an alternate path to the pom can + be specified. + +----- + <artifact:mvn pom="path/to/my-pom.xml" mavenHome="/path/to/maven-2.0.x"> + <arg value="install"/> + </artifact:mvn> +----- + +Running the Mvn Task without a Maven Installation + + If no local maven installation is available, the mvn task will attempt to resolve (download) + the necessary jar files from the central maven repository and run the maven build using + these jar files. + + When the <<<mavenHome>>> attribute is not set, the <<<mvn>>> task will attempt to automatically + resolve the required jar files. + +----- + <artifact:mvn pom="path/to/my-pom.xml"> + <arg value="install"/> + </artifact:mvn> +----- + + <<Note: this will use the 2.0.10 version of the core maven jar files>> + +Using the Java Task + + The java task can be used directly without any need for the maven ant tasks. However, + this method requires that Maven is already installed on the system. A + property called <<<maven.home>>> must be set to point to the local maven installation, + then an ant macro can be defined for calling maven. + +----- + <macrodef name="maven"> + <attribute name="options" default="" /> + <attribute name="goal" /> + <attribute name="basedir" /> + <attribute name="resultproperty" default="maven.result" /> + <element name="args" implicit="true" optional="true" /> + <sequential> + <java classname="org.codehaus.classworlds.Launcher" fork="true" + dir="@{basedir}" resultproperty="@{resultproperty}"> + <jvmarg value="-Xmx512m"/> + <classpath> + <fileset dir="${maven.home}/boot"> + <include name="*.jar" /> + </fileset> + <fileset dir="${maven.home}/lib"> + <include name="*.jar" /> + </fileset> + </classpath> + <sysproperty key="classworlds.conf" value="${maven.home}/bin/m2.conf" /> + <sysproperty key="maven.home" value="${maven.home}" /> + <arg line="--batch-mode @{options} @{goal}" /> + </java> + </sequential> + </macrodef> +----- + + This example defines an ant macro called <<<maven>>>. The macro can then be used in + the build like this: + +----- + <maven basedir="${basedir}" + options="${maven.opts}" + goal="install" + resultproperty="maven.build.result"/> +----- + \ No newline at end of file Propchange: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/examples/mvn.apt ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/examples/mvn.apt ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Modified: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/examples/pom.apt URL: http://svn.apache.org/viewvc/maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/examples/pom.apt?rev=772461&r1=772460&r2=772461&view=diff ============================================================================== --- maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/examples/pom.apt (original) +++ maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/examples/pom.apt Wed May 6 22:53:23 2009 @@ -117,7 +117,7 @@ Using profiles in the POM POM profiles can be activated or deactivated using the nested profile element. For example - to activate a profile called <<my-profile>>. + to activate a profile called <<<my-profile>>>. ----- <artifact:pom id="maven.project" file="pom.xml"> Modified: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/index.apt URL: http://svn.apache.org/viewvc/maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/index.apt?rev=772461&r1=772460&r2=772461&view=diff ============================================================================== --- maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/index.apt (original) +++ maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/apt/index.apt Wed May 6 22:53:23 2009 @@ -57,6 +57,8 @@ * {{{examples/pom.html}Pom task}} + * {{{examples/mvn.html}Mvn task}} + * Getting Help If you have any questions specific to the Ant tasks, please contact the Modified: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/site.xml URL: http://svn.apache.org/viewvc/maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/site.xml?rev=772461&r1=772460&r2=772461&view=diff ============================================================================== --- maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/site.xml (original) +++ maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/site/site.xml Wed May 6 22:53:23 2009 @@ -32,6 +32,7 @@ <item name="dependencies" href="examples/dependencies.html"/> <item name="install, deploy" href="examples/install-deploy.html"/> <item name="pom" href="examples/pom.html"/> + <item name="mvn" href="examples/mvn.html"/> </menu> <menu name="Reference"> <item name="dependencies" href="reference.html#dependencies"/> Added: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/test/pom.xml URL: http://svn.apache.org/viewvc/maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/test/pom.xml?rev=772461&view=auto ============================================================================== --- maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/test/pom.xml (added) +++ maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/test/pom.xml Wed May 6 22:53:23 2009 @@ -0,0 +1,11 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + <groupId>it.ant-tasks</groupId> + <artifactId>simple-pom</artifactId> + <version>1.0</version> + <name>Basic Test Pom</name> + <packaging>pom</packaging> + +</project> Propchange: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/test/pom.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/test/pom.xml ------------------------------------------------------------------------------ svn:executable = * Propchange: maven/ant-tasks/branches/maven-ant-tasks-2.0.x/src/test/pom.xml ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision