java 通过 maven-antrun-plugin 使用 antcontrib <if> 任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4724007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:43:04  来源:igfitidea点击:

Using antcontrib <if> task via maven-antrun-plugin

javamavenant-contribmaven-antrun-plugin

提问by Qwerky

My maven java project uses the maven-antrun-plugin to execute a deploy.xml ant script that deploys my app. The deploy.xml uses the <if>task and this seems to be causing the problem;

我的 maven java 项目使用 maven-antrun-plugin 来执行部署我的应用程序的 deploy.xml ant 脚本。deploy.xml 使用该<if>任务,这似乎是导致问题的原因;

[INFO] Executing tasks
[taskdef] Could not load definitions from resource net/sf/antcontrib/antlib.xml. It could not be found.

deploy:
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An Ant BuildException has occured: The following error occurred while executing this line:
E:\My_Workspace\xxxxxx\xxxxxx\xxxxxxx\deploy.xml:24: Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.

[信息] 正在执行任务
[taskdef] 无法从资源 net/sf/antcontrib/antlib.xml 加载定义。无法找到。

部署:
[信息] --------------------------------------------- ---------------------------
[错误] 构建错误
[信息] --------------- -------------------------------------------------- -------
[INFO] An Ant BuildException has occurred: The following error occurred while
execution this line: E:\My_Workspace\xxxxxx\xxxxxx\xxxxxxx\deploy.xml:24: Problem: failed to create task or type如果
原因:名称未定义。
行动:检查拼写。
行动:检查是否已声明任何自定义任务/类型。
行动:检查任何<presetdef>/<macrodef>声明已经发生。

Here is the antrun plugin config from my pom;

这是我的 pom 中的 antrun 插件配置;

<plugin>
    <inherited>false</inherited>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>remote-deploy</id>
            <phase>install</phase>
            <configuration>
                <tasks>
                    <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.plugin.classpath"/>

                        <property name="compile_classpath" refid="maven.compile.classpath" />
                        <property name="runtime_classpath" refid="maven.runtime.classpath" />
                        <property name="plugin_classpath" refid="maven.plugin.classpath" />

                        <echo message="compile classpath: ${compile_classpath}"/>
                        <echo message="runtime classpath: ${runtime_classpath}"/>
                        <echo message="plugin classpath: ${plugin_classpath}"/>

                        <ant antfile="${basedir}/deploy.xml">
                            <target name="deploy" />
                        </ant>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>ant-contrib</groupId>
            <artifactId>ant-contrib</artifactId>
            <version>1.0b3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-jsch</artifactId>
            <version>1.7.1</version>
        </dependency>
    </dependencies>
</plugin>

.. and here is the relevant section from my deploy.xml;

.. 这是我的 deploy.xml 中的相关部分;

<target name="deploy" if="deploy">
    <if>    <!-- line 24 -->
        <and>

Why I look in my maven repo I can see ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jarand when I look inside the jar I can see net/sf/antcontrib/antcontrib.propertiesso no problem there.

为什么我查看我的 Maven 仓库我可以看到ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar,当我查看罐子里面时,我可以看到net/sf/antcontrib/antcontrib.properties所以没有问题。

When I check the values of maven.compile.classpath, maven.compile.classpathand maven.compile.classpathI can't see any reference to antcontrib, could this be the problem? Why don't they appear when antcontribis defined as a dependancy?

当我检查的数值maven.compile.classpathmaven.compile.classpathmaven.compile.classpath我看不到任何引用antcontrib,这会是什么问题?为什么它们在antcontrib被定义为依赖时不出现?

采纳答案by Qwerky

OK, I've solved it.

好的,我已经解决了。

Moving the dependencies out of the <build><plugin>tag and putting them in with the other project dependencies seems to have done the trick.

将依赖项从<build><plugin>标记中移出并将它们与其他项目依赖项一起放入似乎已经成功。

回答by Vasyl Shchukin

I think it is not a very good idea to add ant to compile classpath in order to run maven plugin.

我认为将 ant 添加到编译类路径以运行 maven 插件不是一个好主意。

I use Maven 3.0.4 and it worked by specifying namespace for ant-contrib tags, for example:

我使用 Maven 3.0.4,它通过为 ant-contrib 标签指定命名空间来工作,例如:

<configuration>
  <target>
    <echo message="The first five letters of the alphabet are:"/>
    <ac:for list="a,b,c,d,e" param="letter" xmlns:ac="antlib:net.sf.antcontrib">
      <sequential>
        <echo>Letter @{letter}</echo>
      </sequential>
    </ac:for>
  </target>
</configuration>

My maven-antrun-plugindependencies:

我的maven-antrun-plugin依赖:

<dependencies>
  <dependency>
    <groupId>ant-contrib</groupId>
    <artifactId>ant-contrib</artifactId>
    <version>1.0b3</version>
    <exclusions>
      <exclusion>
        <groupId>ant</groupId>
        <artifactId>ant</artifactId>
      </exclusion>
    </exclusions>
  </dependency>
  <dependency>
    <groupId>org.apache.ant</groupId>
    <artifactId>ant-nodeps</artifactId>
    <version>1.8.1</version>
  </dependency>
</dependencies>

回答by Gapmeister66

I found that you need to include the ant-contrib dependency inside the plugin which will enable the taskdef tag to find antcontrib.properties

我发现您需要在插件中包含 ant-contrib 依赖项,这将使 taskdef 标签能够找到 antcontrib.properties

  <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>ant-contrib</groupId>
                    <artifactId>ant-contrib</artifactId>
                    <version>20020829</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <id>copy-and-rename-template-files</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target name = "copy-and-rename-template-files">
                            <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
                            <if>
                                <available file="src/main/resources/docker/templates" type="dir"/>
                                <then>
                                    <copy todir="${project.build.directory}/docker">
                                        <fileset dir="src/main/resources/docker/templates">
                                            <include name="**/*"/>
                                        </fileset>
                                    </copy>


                                    <move todir="${project.build.directory}/docker">
                                        <fileset dir="${project.build.directory}/docker">
                                            <include name="*"/>
                                        </fileset>
                                        <mapper>
                                            <regexpmapper from="(.*)project(.*)" to="${project.artifactId}"/>
                                        </mapper>
                                    </move>
                                </then>

                                <else>
                                    <echo>src/main/resources/docker/templates does not exist, skipping processing docker templates</echo>
                                </else>
                            </if>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>

回答by Surasin Tancharoen

another solution would be: keep the ant-contrib-1.0b3.jar to a path and then define it like this

另一种解决方案是:将 ant-contrib-1.0b3.jar 保留到一个路径,然后像这样定义它

<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
        <pathelement location="${runningLocation}/ant-contrib-1.0b3.jar" />
    </classpath>
</taskdef>

then

然后

<target name="doSomething">
    <if>
        <equals arg1="${someProp}" arg2="YES" />
        <then>
            <echo message="It is YES" />
        </then>
        <else>
            <echo message="It is not YES" />
        </else>
    </if>
</target>

I put full code example here which you can download https://www.surasint.com/run-ant-with-if-from-maven/

我在这里放了完整的代码示例,您可以下载https://www.surasint.com/run-ant-with-if-from-maven/