Maven项目依赖于Ant项目

时间:2014-04-11 03:00:12

标签: java maven ant dependencies

我们的主要项目是" common-core",一个Ant项目。这个代码库正在不断发展。

我们有另一个项目"批次"这是一个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命令包装在touch上执行common-core-with-dependencies.jar的脚本中,但我希望避免这种情况。


修改:

我使用了Blaine的答案链接的信息,但结果仍然非常复杂。

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

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

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

  • 有些罐子签了名。我必须从META-INF

  • 中删除签名文件
  • 各种弹簧罐都包含相同的META-INF / spring。*文件。这些需要结合起来。我努力让Ant处理这些重复的名称,但最终还是有点破解。

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>

1 个答案:

答案 0 :(得分:2)

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

这篇文章提供了对此过程的非常好的解释:http://devblog.virtage.com/2013/04/embed-and-run-ant-tasks-and-scripts-from-maven/