【问题标题】:Maven project dependent on Ant projectMaven项目依赖Ant项目
【发布时间】:2014-05-25 00:38:41
【问题描述】:

我们的主要项目是“common-core”,一个 Ant 项目。此代码库正在不断开发中。

我们有另一个项目“batch”,它是一个 Maven 项目。在这个项目中,我们希望使用“common-core”作为依赖项。

我试过在 pom 文件中做这样的事情:

<dependency>
  <groupId>mydomain</groupId>
  <artifactId>ibcore-with-dependencies</artifactId>
  <version>current</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/../common-core/bin/common-core-with-dependencies.jar</systemPath>
</dependency>
...
<plugins>
  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
      <execution>
        <id>validate</id>
        <phase>validate</phase>
        <configuration>
          <tasks>
            <touch file="${project.basedir}/../common-core/bin/common-core-with-dependencies.jar" mkdirs="true" />
          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
      <execution>
        <id>clean</id>
        <phase>clean</phase>
        <configuration>
          <tasks>
            <exec dir="${project.basedir}/../common-core/build" executable="ant" failonerror="true">
              <arg line="clean" />
            </exec>
          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
      <execution>
        <id>build</id>
        <phase>process-resources</phase>
        <configuration>
          <tasks>
            <exec dir="${project.basedir}/../common-core/build" executable="ant" failonerror="true">
              <arg line="single-jar" />
            </exec>
            <copy todir="${project.build.directory}/lib" 
               file="${project.basedir}/../common-core/bin/common-core-with-dependencies.jar" />
          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

但这不起作用,因为如果在构建开始之前common-core-with-dependencies.jar 不存在,验证步骤将失败。也就是说,touch 命令发生得太晚了。

process-resources 阶段的 ant 任务构建 common-core 并创建一个包含 common-core 及其依赖项的一体化 jar。

有没有办法让验证步骤不会因为缺少 common-core-dependencies.jar 而失败,或者在 validate 之前执行一个 ant 步骤?

我有一个变通方法,将 mvn build 命令包装在一个脚本中,该脚本在 common-core-with-dependencies.jar 上执行 touch,但我希望避免这种情况。


编辑:

我使用 Blaine 的回答所链接的信息得到了一些工作,但它最终仍然相当复杂。

common-core 有一个包含构建和测试依赖项的 lib 目录。这些可能与 Maven 选择的版本不同,但众所周知它们可以工作,所以这就是我想要的。

我在common-core(用于构建和测试构建)中添加了两个新的 Ant 目标,它们创建了包含所有依赖 jar(或测试 jar)内容的超级 jar 文件。

不幸的是,这有很多问题:

  • 一些罐子已签名。我必须从 META-INF 中删除签名文件

  • 各种 spring jar 都包含相同的 META-INF/spring.* 文件。这些需要结合起来。我努力让 Ant 处理这些重复的名称,但最终还是做了一些修改。

蚂蚁目标代码:

<target name="single-jar" depends="build"  
        description="Produce a single jar containing core-common plus all its dependencies for use by batch">

    <jar destfile="${core-depends.jar}-temp" filesetmanifest="merge">
        <fileset file="${core.jar}" includes="**/*.class"/>
        <fileset dir="${core.src}" includes="**/*.properties" />
        <zipgroupfileset dir="${core.lib}">
            <include name="**/*.jar" />
            <exclude name="notdeployed/**/*.jar" />
        </zipgroupfileset>
    </jar>

    <!-- The combined jar file contains multiple instances of various spring.* files.
         Many of these need to be concatenated into a single instance.
         The only way I can think of to do this is to extract them with distinct names
         before concatenating them -->

    <delete dir="${core.bin}/spring" quiet="true"/>
    <unzip src="${core-depends.jar}-temp" dest="${core.bin}/spring">
      <patternset>
        <include name="META-INF/spring.*"/>
      </patternset>

        <scriptmapper language="javascript">
          self.addMappedName(source + '.' + (Math.random()));
        </scriptmapper>
    </unzip>
    <mkdir dir="${core.bin}/spring"/> <!-- In case there was nothing to unzip -->

    <concat destfile="${core.bin}/spring.schemas" fixlastline="true">
        <fileset dir="${core.bin}/spring" includes="META-INF/spring.schemas.*" />
    </concat>

    <concat destfile="${core.bin}/spring.factories" fixlastline="true">
        <fileset dir="${core.bin}/spring" includes="META-INF/spring.factories.*" />
    </concat>

    <concat destfile="${core.bin}/spring.handlers" fixlastline="true">
        <fileset dir="${core.bin}/spring" includes="META-INF/spring.handlers.*" />
    </concat>

    <concat destfile="${core.bin}/spring.tooling" fixlastline="true">
        <fileset dir="${core.bin}/spring" includes="META-INF/spring.tooling.*" />
    </concat>

    <jar destfile="${core-depends.jar}">
        <zipfileset dir="${core.bin}" prefix="META-INF/" includes="spring.*" />
        <zipfileset src="${core-depends.jar}-temp" excludes="META-INF/spring.schemas META-INF/spring.handlers META-INF/spring.factories META-INF/spring.tooling META-INF/**/*.SF, META-INF/**/*.DSA, META-INF/**/*.RSA"/>
    </jar>

    <delete dir="${core.bin}/spring" quiet="true"/>
    <delete file="${core-depends.jar}-temp" />
    <delete quiet="true">
        <fileset dir="${core.bin}" includes="spring.*"/>
    </delete>

</target>

【问题讨论】:

    标签: java maven ant dependencies


    【解决方案1】:

    您似乎正在尝试构建一个项目并在同一个 pom.xml 中引用它。相反,尝试创建一个专门用于 ant 构建的新 pom。然后在这个项目中引用该项目作为依赖项。

    这里有一篇文章很好地解释了这个过程:https://web.archive.org/web/20140227062842/http://devblog.virtage.com/2013/04/embed-and-run-ant-tasks-and-scripts-from-maven/

    【讨论】:

    • 我设法通过您提供的链接以及大量实验解决了这个问题。
    • 链接已损坏,这使得这个答案在这个时间点不再很有用。
    猜你喜欢
    • 2018-10-20
    • 1970-01-01
    • 2012-10-25
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 2013-11-25
    相关资源
    最近更新 更多