首页 文章

Maven项目依赖于Ant项目

提问于
浏览
0

我们的主要项目是“共同核心”,一个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构建命令包装在 common-core-with-dependencies.jar 上执行 touch 的脚本中,但我希望避免这种情况 .


Edit:

我使用Blaine的答案链接的信息得到了一些工作,但它仍然变得非常复杂 .

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

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

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

  • 有些 jar 签了名 . 我必须从META-INF剥离签名文件

  • 各种 spring jar 都包含相同的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 回答

相关问题