从 Maven 运行 Ant 任务 [英] Run Ant task from Maven

查看:42
本文介绍了从 Maven 运行 Ant 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Ant 构建一个自定义 jar 库,然后我在 Maven 中将其用作依赖项.

<前><依赖><groupId>test-lib</groupId><artifactId>test-lib</artifactId><version>1.0.0system</scope><systemPath>${basedir}/src/main/webapp/WEB-INF/lib/test-lib-1.0.0.jar</systemPath></依赖>

所以,基本上我现在要做的是:

1) 运行 ant 构建自定义库 (test-lib-1.0.0.jar)
2)运行:mvn compile,使用自定义库等编译我的项目.

是否有一个选项可以让我从马文?我找到了 maven run 插件,这是我的设置:

<前><插件>maven-antrun-plugin<版本>1.4<执行><执行><phase>?????这里放什么?????/phase><配置><任务><ant antfile="${basedir}/build.xml"><目标名称="prepare-test-lib"/><目标><目标>运行

但是,在运行时:mvn compile 它抱怨缺少工件:test-lib-1.0.0.jar.我在 <phase/> 标签中使用了编译、生成资源、...,但似乎没有任何效果.

是否可以使用此插件以某种方式解决此问题?

解决方案

使用 Maven Antrun Plugin 时,Maven 会尝试解决依赖关系以构建 ClassPaths 用于 AntRun 调用,因此您面临鸡和蛋的问题:您无法声明将在 AntRun 期间创建的依赖项需要此依赖项才能运行的执行.这行不通.

我的建议是对您的 test-lib 进行 mavenize,将其包含在您的项目构建中并声明对它的常规依赖.换句话说,我的意思是从 Ant 迁移到 Maven 以构建您的 test-lib 并设置 一个多模块项目.为了更直观"地说明事情,如下所示:

我的项目|-- 我的模块||-- 源代码||`--主要||`--java|`-- pom.xml|-- 测试库||-- 源代码||`--主要||`--java|`-- pom.xml`-- pom.xml

其中 my-project/pom.xml 是带有 <packaging>pom</packaging> 的聚合 pom,并在 < 下列出模块;modules> 元素:

<module>my-module</module><module>test-lib</module></模块>

my-module/pom.xml 声明对 test-lib 工件的依赖:

<依赖><groupId>your.group.id</groupId><artifactId>test-lib</artifactId><version>1.0-SNAPSHOT</version></依赖>

我只是在这里给出一个非常高级的概述,您需要稍微阅读文档以了解详细信息,我无法涵盖所有​​内容.从 Sonatype 的第一本书开始(链接如下).

但这将是正确的方法(并且您不应该(ab)使用 system 范围的依赖项).

参考资料

I'm using Ant to build a custom jar library, which then I'm using in Maven as dependency.

<dependency>
    <groupId>test-lib</groupId>
    <artifactId>test-lib</artifactId>
    <version>1.0.0system</scope>
    <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/test-lib-1.0.0.jar</systemPath>
</dependency>

So, basically what I do now is:

1) run ant to build custom library (test-lib-1.0.0.jar)
2) run: mvn compile, to compile my project using custom library among others.

Is there an option for me to do all this (packaing custom jar & compiling project) from Maven? I've found maven run plugin, and here are my settings:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.4
    <executions>
        <execution>
            <phase>?????what to put here?????/phase>
            <configuration>
                <tasks>
                    <ant antfile="${basedir}/build.xml">
                        <target name="prepare-test-lib" />
                    </ant>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

But, when running: mvn compile it complains about missing artifact: test-lib-1.0.0.jar. I've used compile, generate-resouces,... in <phase/> tag, but nothing seems to work.

Is it possible to solve this somehow using this plugin?

解决方案

When using the Maven Antrun Plugin, Maven tries to resolve the dependencies to build ClassPaths for the AntRun invocation and you thus face a chicken and egg problem : you can't declare a dependency that will be created during AntRun execution that requires this dependency to run. This can't work.

My recommendation would be to mavenize your test-lib, to include it in your project build and to declare a regular dependency on it. In other words, what I mean is moving from Ant to Maven to build your test-lib and setting up a multi-modules project. To illustrate things more "visually", something like this:

my-project
|-- my-module
|   |-- src
|   |   `-- main
|   |       `-- java
|   `-- pom.xml
|-- test-lib
|   |-- src
|   |   `-- main
|   |       `-- java
|   `-- pom.xml
`-- pom.xml

Where my-project/pom.xml is an aggregating pom with a <packaging>pom</packaging> and list the modules under a <modules> element:

<modules>
  <module>my-module</module>
  <module>test-lib</module>
</modules>

And where my-module/pom.xml declares a dependency on the test-lib artifact:

<dependency>
  <groupId>your.group.id</groupId>
  <artifactId>test-lib</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

I'm just giving a very high-level overview here, you need to read the documentation a bit for the details, I can't cover everything. Start with the first book from Sonatype (link below).

But that would be the right way to go (and you should just not (ab)use system scoped dependencies).

References

这篇关于从 Maven 运行 Ant 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆