简体   繁体   中英

building dependencies in a jar type maven project

I have a maven project with package type jar which has some dependencies which will be packed in the resulting jar file when building the project. Some of those dependencies are my own projects which I'd like to automatically re-build when I build the current project. How can this be achieved? I currently only found the <modules> section which can only be used with package type pom, so this seems to be out of the question. Any ideas on this problem? Here a short example of the packages current 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>mygroup</groupId>
    <artifactId>myproject2</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>
    <name>myproject2</name>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>mymainclass</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.15</version>
                <configuration>
                    <enableAssertions>false</enableAssertions>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        [... some external dependencies ...]
        <dependency>
            <groupId>mygroup</groupId>
            <artifactId>myproject1/artifactId>
            <version>0.0.1</version>
        </dependency>
    </dependencies>
</project>

UPDATE:

To elaborate on the development problem I try to solve:

Imagine you have both projects open in your IDE (I use netbeans). You are debugging myproject2 to track down some bug. You navigate by jumping through the code by klicking on Class names or by following the steps in the debugger.

This may often lead to changes in myproject1 without you even noticing it. In result the you would rebuild myproject2 (which you are debugging) without myproject1 being rebuild automatically. The error still happens and you may be clueless why this happens and search and search for another bug until you realize you didn't rebuild myproject1 before. You would then have to rebuild myproject1 afterwards myproject2 and then would have the correct behaviour (or the next bug). This happens quite often at work.

The same can happen with a separate multi-project POM which essentially is another project which will/can be forgotten when working on the code in the IDE.

You can create a multi-module project that includes the project in question and dependent projects as modules. The parent project will be of type "pom" but triggering a build on the parent project will result in a re-build of all modules.

    <groupId>group</groupId>
    <artifactId>artifact</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>dependency.1</module>
        <module>dependency.2</module>
        <module>your.project</module>       
    </modules>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM