简体   繁体   English

在jar类型的Maven项目中建立依赖关系

[英]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. 我有一个打包类型为jar的maven项目,该项目具有一些依赖关系,在构建项目时,这些依赖关系将打包在生成的jar文件中。 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. 我目前仅找到<modules>部分,该部分只能与包类型pom一起使用,因此这似乎是不可能的。 Any ideas on this problem? 关于这个问题有什么想法吗? Here a short example of the packages current pom: 这是当前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). 假设您在IDE中打开了两个项目(我使用netbeans)。 You are debugging myproject2 to track down some bug. 您正在调试myproject2以跟踪一些错误。 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. 这可能经常导致myproject1中的更改​​,而您甚至没有注意到它。 In result the you would rebuild myproject2 (which you are debugging) without myproject1 being rebuild automatically. 结果,您将重建myproject2(正在调试),而不会自动重建myproject1。 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. 该错误仍然会发生,您可能不知道为什么会发生这种情况,并搜索并搜索另一个错误,直到您意识到之前没有重建myproject1。 You would then have to rebuild myproject1 afterwards myproject2 and then would have the correct behaviour (or the next bug). 然后,您必须在myproject2之后重建myproject1,然后才能具有正确的行为(或下一个错误)。 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. 使用单独的多项目POM可能发生相同的情况,该项目实际上是在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. 父项目的类型将为“ pom”,但在父项目上触发构建将导致重新构建所有模块。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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