简体   繁体   English

做Maven构建的Ant任务-但使用与其他Ant脚本不同的Java版本

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

Hi, 你好

I am trying to include a Maven build into my Ant build. 我正在尝试将Maven构建包括到我的Ant构建中。 I have this going fine when I use just the Ant task involved, using the Maven Ant plugin . 当我仅使用涉及的Ant任务(使用Maven Ant插件)时 ,就可以顺利进行。

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. 我的问题是我的Maven代码包括一个1.6依赖的.jar文件,但是我的Ant脚本是1.5依赖的,因此如果使用1.6运行将失败。 So I have to create a process which switches JVM to run my Maven target in my Ant script. 因此,我必须创建一个进程来切换JVM以在我的Ant脚本中运行我的Maven目标。

I have tried: Adding a plugin to the build section of my master POM which customizes the compiler: 我尝试过:在我的主POM的build部分中添加一个插件来自定义编译器:

<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. 这不起作用,因为它说-s有问题。 I've checked and it is a Java 1.6 parameter to javac which does not exist in 1.5! 我已经检查过了,这是javac的Java 1.6参数,在1.5中不存在! So it is creating the javac call for 1.6 and then running it with the 1.5 javac. 因此,它正在为1.6创建javac调用,然后使用1.5 javac运行它。 Also, I would prefer to only be editing the Ant scripts to do this. 另外,我宁愿仅编辑Ant脚本来执行此操作。

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. 我还尝试过<property>和<sysproperty>(在Java调用中)和<arg>的每种组合来设置PATH和JAVA_HOME变量,但是它们似乎无法按照我需要的方式工作。 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. 我什至将<java>标记的JVM属性设置为针对1.6 Java运行,但是Maven调用该Javac似乎仍然存在问题。

Thanks in advance. 提前致谢。

Here's the section from the Maven site (polished slightly) for convenience: 为了方便起见,这是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>

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

You should try using the ant exec Task for this. 您应该尝试使用ant exec Task来实现。 Look here for a previous detailed answer to this: Run ant task in different jvm 在此处查找以前的详细答案: 在不同的jvm中运行ant任务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM