Nice programing

Maven : 공용 저장소에서 찾을 수없는 jar 포함

nicepro 2020. 12. 1. 19:43
반응형

Maven : 공용 저장소에서 찾을 수없는 jar 포함


Maven 공용 저장소에없는 타사 라이브러리를 사용하는 경우 다른 사람이 내 코드를 확인할 때 빌드 할 수 있도록 내 프로젝트에 대한 종속성으로 포함하는 가장 좋은 방법은 무엇입니까?

내 응용 프로그램 "A"는 공용 저장소에없는 jar "B"에 의존합니다. 그러나 "B"를 "A"에 대한 종속성으로 추가하여 지구 반대편에있는 사람이 코드를 확인하고 "A"를 계속 빌드 할 수 있도록하고 싶습니다.


프로젝트를 직접 설치할 수 있습니다.

또는 system다음과 같은 범위 를 사용할 수 있습니다 .

<dependency>
    <groupId>org.group.project</groupId>
    <artifactId>Project</artifactId>
    <version>1.0.0</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/project-1.0.0.jar</systemPath>
</dependency>

systemPath프로젝트의 절대 경로가 필요합니다. 더 쉽게 만들기 위해 jar 파일이 저장소 / 프로젝트 내에있는 ${basedir}경우 프로젝트의 루트에 바인딩 된 속성 을 사용할 수 있습니다 .


이 상황에있는 모듈이있는 상위 프로젝트가있는 경우 (저장소에없는 종속성 필요) 상위 프로젝트를 설정하여 exec-maven-plugin 플러그인을 사용하여 종속 파일을 자동 설치할 수 있습니다. 예를 들어, 공개적으로 사용할 수 없기 때문에 authorize.net jar 파일로이 작업을 수행해야했습니다.

상위 POM :

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <inherited>false</inherited>
            <executions>
                <execution>
                    <id>install-anet</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>mvn</executable>
                <arguments>
                    <argument>install:install-file</argument>
                    <argument>-Dfile=service/lib/anet-java-sdk-1.4.6.jar</argument>
                    <argument>-DgroupId=net.authorize</argument>
                    <argument>-DartifactId=anet-java-sdk</argument>
                    <argument>-Dversion=1.4.6</argument>
                    <argument>-Dpackaging=jar</argument>
                </arguments>
            </configuration>
        </plugin>
    </plugins>
</build>

위의 예에서 jar의 위치는 "service"모듈의 lib 폴더에 있습니다.

서비스 모듈이 유효성 검사 단계에 들어가면 jar를 로컬 저장소에서 사용할 수 있습니다. 상위 pom에서 그룹 ID, 아티팩트 등을 설정하는 방식으로 참조하십시오. 예를 들면 :

<dependency>
    <groupId>net.authorize</groupId>
    <artifactId>anet-java-sdk</artifactId>
    <version>1.4.6</version>
</dependency>

Using system scope may work but it is not recommended even in the Maven specification. it is not portable.

from Maven book:

system- The system scope is similar to provided except that you have to provide an explicit path to the JAR on the local file system. This is intended to allow compilation against native objects that may be part of the system libraries. The artifact is assumed to always be available and is not looked up in a repository. If you declare the scope to be system, you must also provide the systemPath element. Note that this scope is not recommended (you should always try to reference dependencies in a public or custom Maven repository).

The best approach is to install to your local repository or to your enterprise repository to be accessible to all your peers.

this is very easy if you are using a repository manager such as Nexus.


Generally speaking, you should first put the 3rd party jar into your local repository. After that you can use it by adding the dependency into pom.xml.

For example.

1.put the jar into your local repository first:

mvn install:install-file -Dfile=<path-to-file>

Note: this command requires maven-install-plugin version 2.5 or later. If not, You can refer to Here

2.use the jar by adding the dependency into you project's pom.xml.
just add this into the pom.xml of your project:

<dependency>
  <groupId>${the groupId in the jar's pom.xml}</groupId>
  <artifactId>${the artifactId in the jar's pom.xml}</artifactId>
  <version>${the version in the jar's pom.xml}</version>
</dependency>

3.you can then package or deploy your project by running mvn package or mvn deploy

The 3rd party jar will also be included in the package.


If you are using groovy/grail tool suite (GGTS) then you can directly import that third party dependency (but be sure you have that third party dependency in your local repository) using below steps :

  1. Go to the Project Explorer and right click on project.
  2. Click on import option.
  3. Expend the maven option and select Install or deploy an artifact to a maven repository and click next.
  4. Brows and select that third party dependency using Artifact File option and enter the detail of Group Id, Artifact Id and Version using POM.xml file and click on finish

Wait some moment and possibly error would have gone for that problem.


This solution worked for me; 1. Created a local-maven-repo in my project's root directory and copied all my jars in the 2. Executed the following command to generate the necessary pom files and metadata etc for each and every jar that I needed to use;

mvn deploy:deploy-file -DgroupId=<somegroupid> -DartifactId=<someartifact> -Dversion=1.0.0 -Durl=file:./local-maven-repo/ -DrepositoryId=local-maven-repo -DupdateReleaseInfo=true -Dfile=<path to jar file>

This generated a new jar file with a pom file inside the local-maven-repo and I was able to include into my project as a dependency like this;

    <dependency>
        <groupId>somegroupid</groupId>
        <artifactId>someartifact</artifactId>
        <version>1.0.0</version>
    </dependency>

Then mvn package ensured that my project dependencies are resolved and packaged with my war file.

참고URL : https://stackoverflow.com/questions/1355548/maven-including-jar-not-found-in-public-repository

반응형