programing

Maven이 의존관계를 타겟/립에 복사하도록 하다

itsource 2022. 7. 14. 20:48
반응형

Maven이 의존관계를 타겟/립에 복사하도록 하다

프로젝트의 런타임 의존성을 복사하려면 어떻게 해야 합니까?target/lib폴더?

지금 이대로라면mvn clean installtarget폴더에는 내 프로젝트의 jar만 포함되어 있고 런타임 종속성은 포함되어 있지 않습니다.

이것으로 충분합니다.

<project>
  ...
  <profiles>
    <profile>
      <id>qa</id>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <phase>install</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/lib</outputDirectory>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
mvn install dependency:copy-dependencies 

대상 폴더에 생성된 종속성 디렉토리에서 작동합니다.마음에 들어!

최적의 접근법은 무엇을 하고 싶은지에 따라 달라집니다.

  • 종속성을 WAR 또는 EAR 파일로 번들하려면 프로젝트의 패키징 유형을 EAR 또는 WAR로 설정하십시오.Maven은 의존 관계를 올바른 위치에 묶습니다.
  • 모든 의존관계와 함께 코드를 포함하는 JAR 파일을 작성하려면 jar-with-dependencies 기술자와 함께 어셈블리 플러그인을 사용하십시오.Maven은 모든 클래스와 모든 종속성 클래스가 포함된 완전한 JAR 파일을 생성합니다.
  • 의존관계를 인터랙티브하게 타겟디렉토리에 풀 하는 경우는, 의존관계 플러그인을 사용해 파일을 카피합니다.
  • 다른 유형의 처리를 위해 종속성을 가져오려면 자체 플러그인을 생성해야 합니다.종속성 목록과 디스크 상의 위치를 가져오는 API가 있습니다.거기서부터 그걸 가져가야 할 거야...

Maven 의존관계 플러그인, 특히 의존관계: copy-dependencies 목표를 살펴봅니다.Dependency: copy-dependencies mojo라는 제목 아래의 예를 참조하십시오.output Directory 구성 속성을 ${basedir}/target/lib로 설정합니다(테스트를 해야 할 것 같습니다).

이게 도움이 됐으면 좋겠다.

maven의 다른 단계를 사용하지 않고 의존 관계를 대상 디렉토리에 복사해야 하는 경우에 사용하기 위한 심플하고 우아한 솔루션입니다(Vaadin을 사용할 때 매우 유용하다는 것을 알았습니다).

완전한 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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>groupId</groupId>
    <artifactId>artifactId</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>process-sources</phase>

                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>

                            <configuration>
                                <outputDirectory>${targetdirectory}</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
            </plugin>
        </plugins>
    </build>
</project>

그럼 달려라mvn process-sources

jar 파일의 의존관계는 다음 URL에서 확인할 수 있습니다./target/dependency

이 조작을 가끔 실행하는 경우(따라서 POM을 변경하지 않는 경우) 다음 명령줄을 사용해 보십시오.

mvn 의존관계: copy-dependencies -DoutputDirectory=${project.build.directory}/lib

마지막 인수를 생략하면 의존관계는target/dependencies.

의 pom.xml 안에 과 같은 입니다.build/plugins:

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

은, 「 」로 합니다.package「」를 실행하는 "

mvn clean package

즉, outputDirectory는 outputDirectory 。lib★★★★★★★★★★★★★★★★★★,

이 작업을 가끔만 수행할 경우 pom.xml을 변경할 필요가 없습니다.다음 작업을 수행합니다.

mvn clean package dependency:copy-dependencies

「」( 「」)을 ,${project.build.directory}/dependencies 「 속성」을 합니다.outputDirectory 말해 네.

    -DoutputDirectory=${project.build.directory}/lib

다음과 같은 방법을 사용해 보십시오.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
    <archive>
        <manifest>  
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>MainClass</mainClass>
        </manifest>
    </archive>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.4</version>
    <executions>
        <execution>
            <id>copy</id>
            <phase>install</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>
                    ${project.build.directory}/lib
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

가정하다

  • pom.xml을 변경하지 않는 것이 좋습니다.
  • 테스트 범위(예: junit.jar) 또는 제공된 종속성(예: wlfullclient.jar)을 원하지 않습니다.

나에게 효과가 있었던 것은 다음과 같습니다.

mvn 설치 종속성: copy-dependencies -DincludeScope=sys -DoutputDirectory=target/lib

응용 프로그램 jar 번들과 그 모든 종속성 및 MainClass를 호출하는 몇 가지 스크립트를 제공하려면 appassembler-maven-plugin을 참조하십시오.

다음 구성에서는 응용 프로그램을 실행하는 Window 및 Linux용 스크립트를 생성합니다(모든 종속성 jar를 참조하는 생성된 경로를 사용하여 모든 종속성을 다운로드하십시오(타깃/어셈블러 아래의 lib 폴더에).그런 다음 어셈블리 플러그인을 사용하여 전체 Appassembler 디렉토리를 zip으로 패키징할 수 있습니다. zip은 jar와 함께 저장소로 설치/전개됩니다.

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
      <execution>
        <id>generate-jsw-scripts</id>
        <phase>package</phase>
        <goals>
          <goal>generate-daemons</goal>
        </goals>
        <configuration>
          <!--declare the JSW config -->
          <daemons>
            <daemon>
              <id>myApp</id>
              <mainClass>name.seller.rich.MyMainClass</mainClass>
              <commandLineArguments>
                <commandLineArgument>start</commandLineArgument>
              </commandLineArguments>
              <platforms>
                <platform>jsw</platform>
              </platforms>              
            </daemon>
          </daemons>
          <target>${project.build.directory}/appassembler</target>
        </configuration>
      </execution>
      <execution>
        <id>assemble-standalone</id>
        <phase>integration-test</phase>
        <goals>
          <goal>assemble</goal>
        </goals>
        <configuration>
          <programs>
            <program>
              <mainClass>name.seller.rich.MyMainClass</mainClass>
              <!-- the name of the bat/sh files to be generated -->
              <name>mymain</name>
            </program>
          </programs>
          <platforms>
            <platform>windows</platform>
            <platform>unix</platform>
          </platforms>
          <repositoryLayout>flat</repositoryLayout>
          <repositoryName>lib</repositoryName>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-4</version>
    <executions>
      <execution>
        <phase>integration-test</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptors>
            <descriptor>src/main/assembly/archive.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
    </executions>
  </plugin> 

다이어코트리를 zip으로 패키지하는 어셈블리 기술자(src/main/assembly 단위)는 다음과 같습니다.

<assembly>
  <id>archive</id>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
     <directory>${project.build.directory}/appassembler</directory>
     <outputDirectory>/</outputDirectory>
    </fileSet>
  </fileSets>
</assembly>

프로젝트를 전쟁 또는 귀 타입으로 만들면 종속성이 복사됩니다.

의존도가 높은 임베디드 솔루션이지만, Maven의 어셈블리 플러그인이 도움이 됩니다.

@Rich Seller의 답변은 유효합니다.단, 간단한 경우에는 사용 가이드에서 발췌한 내용만 있으면 됩니다.

<project>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.2</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Shad Plugin을 사용하여 모든 타사 종속 제품을 번들할 수 있는 uber 항아리를 만들 수 있습니다.

이미 말한 것을 간단히 설명하자면.코드와 함께 종속성이 포함된 실행 가능한 JAR 파일을 만들고 싶었습니다.이 방법은 효과가 있었습니다.

(1) 폼의 <build> <plugins> 아래에 다음을 포함시켰다.

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-5</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>dk.certifikat.oces2.some.package.MyMainClass</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

(2) mvn compile assembly:assembly를 실행하면 프로젝트의 타깃 디렉토리에 원하는 my-project-0.1-SNAPSHOT-jar-with-dependencies.jar가 생성되었습니다.

(3) java-jar my-project-0.1-SNAPSHOT-jar-with-dependencies를 사용하여 JAR을 실행하였습니다.항아리

Eclipse의 Tomcat 서버에서 실행할 때 WEB-INF/lib 파일에 종속성이 나타나지 않는 것과 관련된 문제가 있는 경우 다음을 참조하십시오.

Tomcat 부팅 시 ClassNotFoundException DispatcherServlet(매븐 의존관계가 wtpwebapps에 복사되지 않음)

Project Properties > Deployment Assembly에서 Maven Dependencies를 추가하면 됩니다.

해서 ㅇㅇㅇㅇㅇㅇㄹㄹㄹㄹㄹㄹㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴsettings.xml다음과 같은 기본 구성을 사용하여 프로젝트 디렉토리에 파일을 저장합니다.

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <localRepository>.m2/repository</localRepository>
    <interactiveMode/>
    <offline/>
    <pluginGroups/>
    <servers/>
    <mirrors/>
    <proxies/>
    <profiles/>
    <activeProfiles/>
</settings>

이러한 설정에 대한 자세한 내용은 Maven 공식 문서에서 확인할 수 있습니다.
절대 경로를 입력하지 않는 한 실제 설정 파일이 있는 디렉토리를 기준으로 경로가 해결됩니다.

maven 명령을 실행할 때 다음과 같이 설정 파일을 사용할 수 있습니다.

mvn -s settings.xml clean install

사이드 노트:저는 GitLab CI/CD 파이프라인에서 이것을 사용하여 여러 작업을 위해 maven 저장소를 캐시하고 모든 작업을 실행할 때 종속성을 다운로드할 필요가 없습니다.GitLab은 당신의 프로젝트 디렉토리에서만 파일이나 디렉토리를 캐시할 수 있기 때문에 나는 내 프로젝트 디렉토리가 아닌 디렉토리를 참조한다.

언급URL : https://stackoverflow.com/questions/97640/make-maven-to-copy-dependencies-into-target-lib

반응형