How to pre build jar which is used as dependency in Maven

I created POM.xml file which worked perfectly until I deleted a 3rd party jar,that is dependent in my project. The problem is that the jar is missing, but it is created using the maven-antrun-plugin. Here is the source of the plugin that creates the 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}srcaion${appname}.app" />
                            </exec>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
</plugin>

Further in the POM.xml file is this:

<dependency>
        <groupId>hellojavaworld</groupId>
        <artifactId>hellojavaworld</artifactId>
        <scope>system</scope>
        <systemPath>${basedir}srcaionhellojavaworld.binhellojavaworld.jar</systemPath>
        <version>1.0</version>
        <optional>true</optional>
</dependency>

Everything works fine if the hellojavaworld.jar is compiled manually,but in case the jar does not exist, there is the error missing artifact.

Question is how to write POM.xml that creates jar that is used as dependency in one POM.xml file without manual intervetions?

Thanks


One solution is to use a multi-module build. The SonaType book has a chapter devoted to the subject.

Basically you would need three POM files.

The parent:

<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>

A POM for hellojavaworld:

<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>

A POM for the consumer of the library:

<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>

You would organize the directory structure like this:

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

Create a multi module maven project: See chapter 6 of Maven by example. The first module would be you hellojavaworld and the second the application which has a dependency on hellojavaworld.

pom.xml:

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

application/pom.xml

...
<dependencies>
  <dependency>
    <groupId>hellojavaworld</groupId>
    <artifactId>hellojavaworld</artifactId>
    <version>${project.version}</version>
  </dependency>
</dependencies>
...
链接地址: http://www.djcxy.com/p/29664.html

上一篇: Maven安装更改jar

下一篇: 如何预先构建在Maven中用作依赖项的jar