Using Java project as jar file in other project as

2019-08-20 10:10发布

问题:

I want to use one maven project as dependency using POM.xml in other project with few limitations.

My Maven project1 is framework project. I have created a jar file for that project. This project will reside in a private git repo. I donot want to expose this project other user to edit it.

In my maven project2 or maven project3 i want to use the maven project1 as dependency. the jar file should be downladed to maven dependencies during run time.

Note: the user will not have mave project1 in his eclipse.

For example: when i add testNG dependency to my POM.xml testNG jar will be downloaded to my project similarly, I need to add my maven project1 as dependency to any other maven project and it should downlod project1 as jar.

Please do let me know how to achieve it.

回答1:

I guess you should upload your project to the Maven repository: https://maven.apache.org/guides/mini/guide-central-repository-upload.html



回答2:

You should be able to add that jar to your local repository using this within your pom:

<plugin>
    <executions>
        <execution>
            <id>jarid_to_install</id>
            <phase>package</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <file>resources/jar.jar</file>
                <groupId>com.name</groupId>
                <artifactId>jar</artifactId>
                <version>x.x</version>
                <packaging>jar</packaging>
            </configuration>
        </execution>
    </executions>
</plugin>

After installing it, the jar can be implemented like any other dependency.

    <dependency>
        <groupId>com.name</groupId>
        <artifactId>jar</artifactId>
        <version>x.</version>
        <scope>runtime</scope>
    </dependency>


回答3:

You can set up a private maven repository to host the jar file. More setup information can be found under https://maven.apache.org/guides/introduction/introduction-to-repositories.html

e.g. an example pom.xml in project2 / project3 using the project1 as dependency:

<project>
  ...
  <repositories>
    <repository>
      <id>my-internal-site</id>
      <url>http://myserver/repo</url>
    </repository>
  </repositories>
  ...
</project>

Or just use JFrog Artifactory for hosting any dependencies: https://www.jfrog.com/open-source/



回答4:

You can use

mvn install:install-file -DgroupId=projecta -DartifactId=projecta -Dversion=1.0.0 -Dpackaging=jar -Dfile=path to you .jar file here.

You can install your jar into your local m2 repository by using this command and then access it like any other dependency in your projectB by using the groupid, artifactid and version you defined in the command above.