【问题标题】:How to pre build jar which is used as dependency in Maven如何预先构建在 Maven 中用作依赖项的 jar
【发布时间】:2015-10-02 12:19:55
【问题描述】:

我创建了 POM.xml 文件,该文件运行良好,直到我删除了依赖于我的项目的第 3 方 jar。问题是 jar 丢失了,但它是使用 maven-antrun-plugin 创建的。 以下是创建 jar 的插件的源代码:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <configuration>
                        <tasks>
                            <property environment="env" />
                            <property name="appname" value="hellojavaworld" />
                            <echoproperties />
                            <exec dir="" executable="respawn.exe" searchpath="true">
                                <arg value="${basedir}\src\aion\${appname}.app" />
                            </exec>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
</plugin>

在 POM.xml 文件中进一步是这样的:

<dependency>
        <groupId>hellojavaworld</groupId>
        <artifactId>hellojavaworld</artifactId>
        <scope>system</scope>
        <systemPath>${basedir}\src\aion\hellojavaworld.bin\hellojavaworld.jar</systemPath>
        <version>1.0</version>
        <optional>true</optional>
</dependency>

如果手动编译 hellojavaworld.jar 一切正常,但如果 jar 不存在,则会出现错误缺少工件。

问题是如何编写 POM.xml 来创建一个 jar 用作一个 POM.xml 文件中的依赖项而无需手动干预?

谢谢

【问题讨论】:

    标签: java xml maven jar


    【解决方案1】:

    一种解决方案是使用多模块构建。 SonaType 书籍有一个专门用于该主题的chapter。

    基本上你需要三个 POM 文件。

    父母:

    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                                 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.sonatype.mavenbook.multi</groupId>
        <artifactId>parentHoldsItTogether</artifactId>
        <packaging>pom</packaging>
        <version>1.0</version>
        <name>Multi Chapter Simple Parent Project</name>
    
        <modules>
            <module>hellojavaworld</module>
            <module>thingThatNeedsHellojavaworld</module>
        </modules>
    </project>
    

    hellojavaworld 的 POM:

    <project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.sonatype.mavenbook.multi</groupId>
            <artifactId>parentHoldsItTogether</artifactId>
            <version>1.0</version>
        </parent>
        <artifactId>hellojavaworld</artifactId>
        <packaging>jar</packaging>
    </project>
    

    图书馆消费者的 POM:

    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                                 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.sonatype.mavenbook.multi</groupId>
            <artifactId>parentHoldsItTogether</artifactId>
            <version>1.0</version>
        </parent>
    
        <artifactId>thingThatNeedsHellojavaworld</artifactId>
        <packaging>???</packaging>
        <dependencies>
            <dependency>
                <groupId>org.sonatype.mavenbook.multi</groupId>
                <artifactId>hellojavaworld</artifactId>
                <version>1.0</version>
            </dependency>
        </dependencies>    
    </project>
    

    您可以像这样组织目录结构:

    your_project_folder/
        pom.xml                        <-- the parent POM
        hellojavaworld/
            pom.xml                 
            src/main/java
        thingThatNeedsHellojavaworld/
            pom.xml
            src/main/java
    

    【讨论】:

      【解决方案2】:

      创建一个多模块的maven项目:见chapter 6的Maven例子。 第一个模块是你的 hellojavaworld,第二个是依赖于 hellojavaworld 的应用程序。

      pom.xml:

      <project>
      ...
      <modules>
        <module>hellojavaworld</module>
        <module>application</module>
      </modules>
      ...
      

      应用程序/pom.xml

      ...
      <dependencies>
        <dependency>
          <groupId>hellojavaworld</groupId>
          <artifactId>hellojavaworld</artifactId>
          <version>${project.version}</version>
        </dependency>
      </dependencies>
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-19
        • 2020-05-08
        • 1970-01-01
        相关资源
        最近更新 更多