Maven jar dependencies and relations

妖精的绣舞 提交于 2019-12-13 02:49:31

问题


I tried to search for existing questions but cant find any - feels like my question is quite simple but probably because it's quite specific I cant find the answers anywhere.

Anyways - I have 2 projects with Maven where the second depends on/needs some classes of the first. In certain cases I want the dependency to be on the JAR rather than a project dependency.

I solved this by creating an additional jar with maven-jar-plugin, where i included the needed classes. The jar was correctly created and was output in ${project.build.directory}. But the pom of project A is located in the same directory, that is/was a pain for me and for project B, cause of the dependencies used in project A. (is there a way to ignore the pom located in ${project.build.directory} ??? like:

<dependency>
            <groupId>projectA.groupId</groupId>
            <artifactId>A</artifactId>
            <version>1x-SNAPSHOT</version>
            <classifier>classes</classifier>
                    ->  <ignorePom>true</ignorePom>
        </dependency>

).

Now I´m using the system scope for the dependency and its working fine after adding the systempath. Although the scope system must be avoided everywhere it is possible. The Systempath is annoying me because this is not a good practice and because of the absolute path.

I know that i can install the created jar into the repository by using

mvn install:install -file

but i want to automate this process as good as possible, what would you suggest?

thanks in advance


回答1:


The best solution is to use a repository manager and do a real release of the first jar and make a real dependency where you need.

Based on what you wrote i would recommend to create a separate maven project which contains the classes which will be used by several other modules make a release of it and simply use a dependency on that module. This will solve your problem completely.




回答2:


Maven does not support 'dependencies on jars'. There is the 'system' scope, but it's very limited and can lead to all kinds of trouble. If you need to depend on a Jar, you need to assign it a unique groupId and artifactId. If you don't want to build it in Maven, you can deploy it to your local repo manager (or even to your local repo with mvn install:install-file).

Depending on jars with classifiers 'works' but not very well, because the reactor can't include them in a multi-module project properly.




回答3:


Have you read any of the documentation regarding Maven at all? If you have your projects defined using Maven then you don't have to do any special to get the dependencies to work.

For your needs you have two choices: Create two separate projects where one depends on the other OR you can create a multi-module project where one module still depends on the other.

Two separate projects

.
 |-- A
 |   `-- pom.xml
 |   `-- src
 |       `-- main
 |           `-- java
 `-- B
     `-- pom.xml
     `-- src
         `-- main
             `-- java

A/pom.xml

<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>com.company</groupId>
    <artifactId>A</artifactId>
    <version>1.0-SNAPSHOT</version>
</project>

B/pom.xml

<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>com.company</groupId>
    <artifactId>B</artifactId>
    <version>2.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.company</groupId>
            <artifactId>A</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

That's it! You have a dependency on project/module A in B.

You build project A first by issuing mvn install (and being in directory A). The artifact will be installed in your local repo (and available for other projects).

Then you build project B by issuing mvn install (and being in directory B). The artifact will be installed in your local repo.

One parent project with sub-modules

.
 |-- A
 |   `-- pom.xml
 |   `-- src
 |       `-- main
 |           `-- java
 `-- B
 |   `-- pom.xml
 |   `-- src
 |       `-- main
 |           `-- java
 `-- pom.xml

pom.xml

<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>com.company</groupId>
    <artifactId>parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>A</module>
        <module>B</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.company</groupId>
                <artifactId>A</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

The <depencdencyManagement/> is there to help sub-modules pick the correct versions.

A/pom.xml

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

    <parent>
        <groupId>com.company</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.company</groupId>
    <artifactId>A</artifactId>

</project>

B/pom.xml

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

    <parent>
        <groupId>com.company</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.company</groupId>
    <artifactId>B</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.company</groupId>
            <artifactId>A</artifactId>
        </dependency>
    </dependencies>
</project>

With this solution you only have to position yourself in the parent folder and run mvn install (or mvn package or the like). You will now build both A and B in the correct order and this whole project can be imported in Eclipse for example.



来源:https://stackoverflow.com/questions/10721639/maven-jar-dependencies-and-relations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!