Ant task to do a Maven build - but use a different version of Java than the rest of the Ant script

StackOverflow https://stackoverflow.com/questions/21261539

  •  30-09-2022
  •  | 
  •  

Domanda

Hi,

I am trying to include a Maven build into my Ant build. I have this going fine when I use just the Ant task involved, using the Maven Ant plugin.

My problem is that my Maven code includes a 1.6 dependent .jar file, but my Ant script is 1.5 dependent so will fail if run with 1.6. So I have to create a process which switches JVM to run my Maven target in my Ant script.

I have tried: Adding a plugin to the build section of my master POM which customizes the compiler:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.5.1</version>
    <configuration>
        <executable>"c:\Program Files (x86)\Java\jdk1.6.0_45\bin\javac.exe"</executable>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
</plugin>

This does not work as it says it has a problem with -s. I've checked and it is a Java 1.6 parameter to javac which does not exist in 1.5! So it is creating the javac call for 1.6 and then running it with the 1.5 javac. Also, I would prefer to only be editing the Ant scripts to do this.

I have also tried to every combination of <property> and <sysproperty> (in the Java call) and <arg> to set the PATH and JAVA_HOME variables but they don't seem to work the way I need them to. Is it that this is just not possible? I even set the JVM attribute of the <java> tag to run against the 1.6 java but it still seems to have issues with the javac the Maven is calling to do the build.

Thanks in advance.

Here's the section from the Maven site (polished slightly) for convenience:

<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>

<target name="my_maven_target">
    <maven basedir="${basedir}" options="${maven.opts}" goal="install" resultproperty="maven.build.result"/>
</target>

È stato utile?

Soluzione

You should try using the ant exec Task for this. Look here for a previous detailed answer to this: Run ant task in different jvm

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top