Maven을 사용하여 종속성이 있는 실행 가능한 JAR을 작성하려면 어떻게 해야 합니까?
배포하기 위해 프로젝트를 단일 실행 가능 JAR로 패키지화하려고 합니다.
Maven 프로젝트 패키지의 모든 종속성 JAR을 출력 JAR로 만들려면 어떻게 해야 합니까?
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
그리고 당신은 그것을
mvn clean compile assembly:single
컴파일 목표는 assembly:single 전에 추가해야 합니다.그렇지 않으면 자신의 프로젝트 코드가 포함되지 않습니다.
상세한 것에 대하여는, 코멘트를 참조해 주세요.
일반적으로 이 목표는 자동으로 실행되는 빌드 단계에 관련됩니다.에 의해, JAR 의 가 됩니다.mvn install
★★★★★★★★★★★★★★★★★★★★★★★★★★★
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
dependency-plugin을 사용하면 패키지 단계 전에 별도의 디렉터리에 모든 종속성을 생성한 후 이를 매니페스트의 클래스 경로에 포함할 수 있습니다.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>theMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
" " 를 사용합니다.${project.build.directory}/classes/lib
모든 jar 파일을 메인 jar에 통합하려면 OutputDirectory로 지정합니다.단, jar를 로드하려면 커스텀 클래스 로딩 코드를 추가해야 합니다.
"executable-jar-with-maven-example"(GitHub) 참조
메모들
그 장단점들은 Stephan에 의해 제공되었다.
수동 도입의 경우
- 장점
- 단점
- 의존관계는 마지막 병에서 벗어났다.
종속성을 특정 디렉터리에 복사
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}.lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Jar 실행 파일 및 클래스 경로 인식 설정
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>${project.build.finalName}.lib/</classpathPrefix>
<mainClass>${fully.qualified.main.class}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
에서, 「」는jar
는 실제로 외부 클래스 패스 요소를 사용하여 실행할 수 있습니다.
$ java -jar target/${project.build.finalName}.jar
전개 가능한 아카이브 만들기
jar
만 실행할 수 ....lib/
디렉토리로 이동합니다.디렉토리 및 그 컨텐츠와 함께 전개할 아카이브를 작성할 필요가 있습니다.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>antrun-archive</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<property name="final.name" value="${project.build.directory}/${project.build.finalName}"/>
<property name="archive.includes" value="${project.build.finalName}.${project.packaging} ${project.build.finalName}.lib/*"/>
<property name="tar.destfile" value="${final.name}.tar"/>
<zip basedir="${project.build.directory}" destfile="${final.name}.zip" includes="${archive.includes}" />
<tar basedir="${project.build.directory}" destfile="${tar.destfile}" includes="${archive.includes}" />
<gzip src="${tar.destfile}" destfile="${tar.destfile}.gz" />
<bzip2 src="${tar.destfile}" destfile="${tar.destfile}.bz2" />
</target>
</configuration>
</execution>
</executions>
</plugin>
, 제가 나왔습니다.target/${project.build.finalName}.(zip|tar|tar.bz2|tar.gz)
.jar
★★★★★★★★★★★★★★★★★」lib/*
.
Apache Maven 어셈블리 플러그인
- 장점
- 단점
- 클래스 재배치가 지원되지 않습니다(클래스 재배치가 필요한 경우 maven-shade-plugin 사용).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>${fully.qualified.main.class}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
있다target/${project.bulid.finalName}-jar-with-dependencies.jar
.
Apache Maven Shade 플러그인
- 장점
- 단점
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${fully.qualified.main.class}</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
있다target/${project.build.finalName}-shaded.jar
.
외야르매븐의
- 장점
- 단점
- 2012년 이후 적극적으로 지원되지 않습니다.
<plugin>
<!--groupId>org.dstovall</groupId--> <!-- not available on the central -->
<groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<executions>
<execution>
<configuration>
<mainClass>${fully.qualified.main.class}</mainClass>
<attachToBuild>true</attachToBuild>
<!-- https://code.google.com/p/onejar-maven-plugin/issues/detail?id=8 -->
<!--classifier>onejar</classifier-->
<filename>${project.build.finalName}-onejar.${project.packaging}</filename>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
스프링 부트 메이븐 플러그인
- 장점
- 단점
- 불필요한 스프링 및 스프링 부트 관련 클래스를 추가합니다.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>${fully.qualified.main.class}</mainClass>
</configuration>
</execution>
</executions>
</plugin>
있다target/${project.bulid.finalName}-spring-boot.jar
.
Unanswered의 답변을 받아들여 다시 포맷하면 다음과 같은 이점이 있습니다.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
다음으로, 이것을 명시적으로 부르는 것보다 자연스럽게 빌드하는 것을 추천합니다.빌드에 이 을 에 합니다.pom.xml
해서 묶는 거예요.package
하는 것은 '갓차'라고 것은 '갓차', '갓차'라고 하는 입니다.assembly:single
이를 pom.xml에 넣는 경우 목표를 설정하고 명령줄에서 수동으로 실행하는 경우 'assembly:assembly'를 호출합니다.
<project>
[...]
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
[...]
</plugins>
[...]
</build>
</project>
maven-shade-plugin을 사용하여 모든 종속성을 하나의 uber-jar로 패키지합니다.메인 클래스를 지정하여 실행 가능한 jar를 작성하는 데도 사용할 수 있습니다.maven-assembly와 maven-jar를 사용해 본 결과, 이 플러그인이 제 요구에 가장 적합하다는 것을 알게 되었습니다.
이 플러그인은 특정 파일의 내용을 덮어쓰는 대신 병합하기 때문에 특히 유용합니다.이는 여러 jars에 동일한 이름을 가진 리소스 파일이 있고 플러그인이 모든 리소스 파일을 패키징하려고 할 때 필요합니다.
아래의 예를 참조해 주세요.
<plugins>
<!-- This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<!-- signed jars-->
<excludes>
<exclude>bouncycastle:bcprov-jdk15</exclude>
</excludes>
</artifactSet>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!-- Main class -->
<mainClass>com.main.MyMainClass</mainClass>
</transformer>
<!-- Use resource transformers to prevent file overwrites -->
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>properties.properties</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
<resource>applicationContext.xml</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/cxf/cxf.extension</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
<resource>META-INF/cxf/bus-extensions.xml</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
maven-shade 플러그인을 사용하여 아래와 같은 uber jar를 만들 수 있습니다.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
오랫동안 maven 어셈블리 플러그인을 사용했지만 문제의 해결책을 찾을 수 없었습니다.지금 다른 플러그인 onejar-maven-plugin을 사용하고 있습니다.아래의 예(mvn package
( ( ) : (이행)'):
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.3.0</version>
<executions>
<execution>
<configuration>
<mainClass>com.company.MainClass</mainClass>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
해당 플러그인에 대한 저장소를 추가해야 합니다.
<pluginRepositories>
<pluginRepository>
<id>onejar-maven-plugin.googlecode.com</id>
<url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>
</pluginRepositories>
maven-dependency-plugin을 사용할 수 있지만 문제는 실행 가능한 JAR을 어떻게 작성하느냐였습니다.그러기 위해서는 Matthew Franglen의 응답을 다음과 같이 변경해야 합니다(btw, 의존관계 플러그인을 사용하면 클린 타깃에서 시작할 때 빌드하는 데 시간이 더 오래 걸립니다).
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>${basedir}/target/dependency</directory>
</resource>
</resources>
</build>
pom.xml에 다음 항목을 추가할 수 있습니다.
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.mycompany.package.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.mycompany.package.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
그런 다음 콘솔을 통해 pom.xml이 있는 디렉토리로 전환해야 합니다.그런 다음 mvn assembly:single을 실행해야 합니다.그러면 의존관계가 있는 실행 가능한 JAR 파일이 구축될 것입니다.cd ./target을 사용하여 출력(타깃) 디렉토리로 전환하고 jara - jar mavenproject1-1.0-SNAPSHOT-jar-with-dependencies.jar와 유사한 명령어로 jar를 시작할 때 확인할 수 있습니다.
Apache Maven 3.0.3으로 테스트했습니다.
다른 JAR의 내용을 1개의 JAR로 재패키지 하는 경우는, Maven Assembly 플러그 인을 사용할 수 있습니다.모든 것을 압축 해제하고, 그 후 를 개입시켜 디렉토리에 재패킹 합니다.<unpack>true</unpack>
그리고 두 번째 패스를 얻어서 하나의 거대한 JAR에 내장할 수 있습니다.
다른 옵션은 OneJar 플러그인입니다.이렇게 하면 위의 재패키지 작업이 모두 한 번에 수행됩니다.
모든 의존관계가 포함된 대용량 실행 가능 항아리를 만들려고 이 모든 응답을 검토했지만 제대로 작동하지 않았습니다.답은 쉐이드 플러그인으로 매우 쉽고 간단합니다.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>path.to.MainClass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
종속성이 올바르게 작동하려면 컴파일 또는 런타임 범위가 있어야 합니다.
'어울리다'를 해서 볼 수 요.maven-shade-plugin
★★★★★★★★★★★★★★★★★」maven-jar-plugin
.
maven-shade-plugin
클래스 및 모든 종속성을 단일 jar 파일로 정리합니다.- 설정합니다.
maven-jar-plugin
실행 파일의 기본 클래스를 지정하려면 "클래스 경로 설정", "Jar 실행 파일 만들기" 장을 참조하십시오.
「POM」의 POM maven-jar-plugin
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.example.MyMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
마지막으로 다음을 호출하여 실행 가능한 jar를 만듭니다.
mvn clean package shade:shade
내 생각에 켄 류는 옳다.maven 종속성 플러그인을 사용하면 모든 종속성을 확장할 수 있으며, 이를 리소스로 처리할 수 있습니다.이를 통해 기본 아티팩트에 포함할 수 있습니다.어셈블리 플러그인을 사용하면 수정이 어려울 수 있는 보조 아티팩트가 생성됩니다(예: 커스텀 매니페스트 엔트리를 추가하려고 했습니다).내 폼은 결국:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
...
<resources>
<resource>
<directory>${basedir}/target/dependency</directory>
<targetPath>/</targetPath>
</resource>
</resources>
</build>
...
</project>
다음과 같이 해야 합니다.
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
패키지 단계일 경우 리소스로 포함되지 않기 때문에 패키지 해제는 리소스 생성 단계에 있어야 합니다.깨끗한 포장을 시도하면 알 수 있습니다.
maven-assembly-plugin-2.2.1에서 공유 어셈블리 파일을 찾는 데 문제가 있습니까?
Descriptor/descriptor 또는 DescriptorRef 매개 변수 대신 descriptorId 구성 매개 변수를 사용해 보십시오.
둘 다 필요한 작업을 하지 않습니다.클래스 경로에서 파일을 찾습니다.물론 공유 어셈블리가 있는 패키지를 maven-assembly-plugin 클래스 경로에 추가해야 합니다(아래 참조).Maven 2.x(Maven 3.x가 아님)를 사용하는 경우 pluginManagement 섹션의 최상위 부모 pom.xml에 이 종속성을 추가해야 할 수 있습니다.
상세한 것에 대하여는, 여기를 참조해 주세요.
클래스: org.apache.maven.plugin.assembly.io 를 참조해 주세요.Default Assembly Reader(기본 어셈블리 리더)
예:
<!-- Use the assembly plugin to create a zip file of all our dependencies. -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorId>assembly-zip-for-wid</descriptorId>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>cz.ness.ct.ip.assemblies</groupId>
<artifactId>TEST_SharedAssemblyDescriptor</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
이 문제를 해결하기 위해 Maven Assembly Plugin을 사용하여 종속 JAR과 함께 단일 실행 가능한 JAR 파일로 JAR을 작성합니다.pom.xml 파일에 다음 플러그인 구성을 추가합니다.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.your.package.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
이 작업을 수행한 후에는 mvn clean compile assembly: single 명령을 사용하여 MAVEN 도구를 실행하는 것을 잊지 마십시오.
다른 사람들이 이미 그렇게 했기 때문에 질문에 직접 대답하지는 않겠지만, 프로젝트의 독 자체에 모든 의존성을 포함시키는 것이 좋은 생각인지 정말 궁금합니다.
요점(도입의 용이성/사용의 용이성)은 이해하지만, 목적의 사용 예에 따라 다릅니다(대안도 있을 수 있습니다(아래 참조).
완전히 독립형으로 사용한다면 왜 안 될까요?
그러나 프로젝트를 다른 컨텍스트(Webapp이나 다른 항아리가 있는 폴더 등)에서 사용하는 경우 클래스 경로(폴더에 있는 항아리, 항아리 안에 있는 항아리)에 중복이 있을 수 있습니다.아마 입찰은 아닐지 몰라도 나는 보통 이것을 피한다.
좋은 대안:
- 응용 프로그램을 .zip / .war로 전개: 아카이브에는 프로젝트의 jar와 모든 의존적인 jar가 포함됩니다.
- 동적 클래스 로더 메커니즘(스프링 참조)을 사용하여 프로젝트의 단일 진입점(시작할 단일 클래스 - 다른 답변의 매니페스트 메커니즘 참조)을 가지며, 이 경우 필요한 다른 모든 항이 현재 클래스 경로에 추가됩니다(동적으로).
이렇게 매니페스트와 "특수 동적 클래스 로더 메인"만 있으면 프로젝트를 시작할 수 있습니다.
java -jar ProjectMainJar.jar com.stackoverflow.projectName.MainDynamicClassLoaderClass
나에게 효과가 있었던 것은, 다음과 같다.
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>SimpleKeyLogger</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
나는 시스템 1에 의존했기 때문에 특별한 경우를 겪었다.
<dependency>
..
<scope>system</scope>
<systemPath>${project.basedir}/lib/myjar.jar</systemPath>
</dependency>
@user189057에 의해 제공된 코드를 변경했습니다.변경사항: 1) maven-dependency-plugin은 "prepare-package" 단계에서 실행됩니다.2) "target/classes"로 직접 압축 해제된 클래스를 추출합니다.
이게 내가 찾은 최선의 방법이야
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.myDomain.etc.MainClassName</mainClass>
<classpathPrefix>dependency-jars/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/dependency-jars/
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
에서는, 모든 가, 「Dependency」의 「Dependency는 「Dependency」에 ./dependency-jars
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★Main
1개이지만, 중 에 「」가 .Main
클래스)com.myDomain.etc.MainClassName
해, JMX 「」, 「」를 start
★★★stop
파라미터를 지정합니다.이것으로, 다음과 같이 애플리케이션을 기동할 수 있었습니다.
java -jar ./lib/TestApp-1.0-SNAPSHOT.jar start
여러분 모두에게 도움이 되길 바랍니다.
이 투고에 기재되어 있는 트리 플러그인을 비교했습니다.2개의 항아리와 모든 항아리가 포함된 디렉토리를 생성했습니다.결과를 비교해보니 메이븐쉐이드 플러그인이 단연 최고입니다.통합이 필요한 여러 스프링 리소스와 jax-rs 및 JDBC 서비스를 보유하고 있다는 것이 과제였습니다.모두 maven-assembly-plugin과 비교하여 쉐이드 플러그인에 의해 올바르게 병합되었습니다.이 경우 스프링을 자신의 리소스 폴더에 복사하고 수동으로 한 번 병합하지 않으면 스프링이 실패합니다.양쪽 플러그인이 올바른 의존관계 트리를 출력한다.테스트, 제공, 컴파일 등 여러 범위를 가지고 있었는데 양쪽 플러그인이 모두 건너뛰었습니다.둘 다 같은 매니페스트를 작성했지만, 변압기를 사용하여 쉐이드 플러그인으로 라이선스를 통합할 수 있었습니다.maven-dependency-plugin을 사용하면 당연히 이러한 문제가 발생하지 않습니다. 왜냐하면 항아리가 추출되지 않기 때문입니다.그러나 다른 지적과 마찬가지로 올바르게 작동하려면 추가 파일 하나를 휴대해야 합니다.다음은 pom.xml의 일부입니다.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<includeScope>compile</includeScope>
<excludeTransitive>true</excludeTransitive>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.rbccm.itf.cdd.poller.landingzone.LandingZonePoller</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<shadedArtifactAttached>false</shadedArtifactAttached>
<keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/services/javax.ws.rs.ext.Providers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.factories</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.tooling</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer">
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
uber-jar에서 특정 종속성을 제외하기 위한 옵션을 찾고 있는 모든 사용자에게 이 솔루션은 도움이 되었습니다.
<project...>
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>1.6.1</version>
<scope>provided</scope> <=============
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>...</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
따라서 mvn-assembly-plugin 설정이 아니라 종속성 속성입니다.
수백만 , 싶었어요.이치노<mainClass>
entryPoint를 응용 프로그램에 추가할 필요가 없는 경우.예를 들어 API에 반드시 메서드가 있는 것은 아닙니다.
maven 플러그인 구성
<build>
<finalName>log-enrichment</finalName>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
구축하다
mvn clean compile assembly:single
확인하다
ll target/
total 35100
drwxrwx--- 1 root vboxsf 4096 Sep 29 16:25 ./
drwxrwx--- 1 root vboxsf 4096 Sep 29 16:25 ../
drwxrwx--- 1 root vboxsf 0 Sep 29 16:08 archive-tmp/
drwxrwx--- 1 root vboxsf 0 Sep 29 16:25 classes/
drwxrwx--- 1 root vboxsf 0 Sep 29 16:25 generated-sources/
drwxrwx--- 1 root vboxsf 0 Sep 29 16:25 generated-test-sources/
-rwxrwx--- 1 root vboxsf 35929841 Sep 29 16:10 log-enrichment-jar-with-dependencies.jar*
drwxrwx--- 1 root vboxsf 0 Sep 29 16:08 maven-status/
명령줄 자체에서 실행 가능한 JAR을 생성하려면 프로젝트 경로에서 다음 명령을 실행하십시오.
mvn assembly:assembly
여기서 가장 많이 투표한 답변을 시도했고, 병을 돌릴 수 있었습니다.하지만 프로그램이 제대로 실행되지 않았다.나는 그 이유가 무엇이었는지 모른다.에서 Eclipse
다른 결과가 나오지만 명령줄에서 jar를 실행하면 다른 결과가 나옵니다(프로그램 고유의 런타임 오류로 인해 크래시됩니다).
프로젝트에 대한 의존관계가 너무 많다는 것만으로 OP와 비슷한 요건을 가지고 있었습니다. 유일한 은 '다다다다다다다'를 사용한 것이다.Eclipse
매우 단순하고 매우 직설적이다. 비슷한 많은 Maven을 가진 입니다.
Eclipse하여 1) 폴더(Eclipse)를 선택합니다.Export
) [ 2) ]를 합니다.Java
->Runnable Jar
3) jar 파일의 위치를 선택하도록 요구됩니다.
를 선택하고 Main을 선택합니다.Package dependencies with the Jar file
다음 을 클릭합니다.Finish
이것도 선택사항이 될 수 있어요jar 파일을 빌드할 수 있습니다.
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<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>WordListDriver</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
pom.xml에 추가:
<dependency>
<groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
</dependency>
그리고.
<plugin>
<groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
바로 그거야.다음 mvn 패키지에서는 모든 종속성 jar를 포함하여 하나의 fat jar가 추가로 생성됩니다.
복수의 솔루션을 시험해 보았습니다만, 그 중 하나는, 사전에 테스트한 관련성이 없는 외부 시스템의 모든 내부 의존성을 가지는, 실행 불가능한 fat jar를 작성하는 시나리오에서 완전하게 동작했습니다.
이것을 pom.xml에 포함하다
<?xml version="1.0" encoding="UTF-8"?>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
fat jar를 빌드하기 위해 실행하는 명령어 ->> mvn 어셈블리: assembly
메이븐 어셈블리 플러그인이 효과가 좋았습니다.maven-dependency-plugin을 몇 시간 동안 사용했지만 제대로 작동하지 않았습니다.주된 이유는 매뉴얼에 기재되어 있는 바와 같이 아티팩트 아이템을 설정 섹션에 명시적으로 정의해야 했기 때문입니다.예를 들어 다음과 같이 사용하는 경우의 예를 제시하겠습니다.mvn dependency:copy
아티팩트 아이템은 포함되어 있지 않지만 동작하지 않습니다.
이 블로그 투고에서는 maven-jar 플러그인과 maven-assembly 플러그인을 조합한 또 다른 접근방식을 소개하고 있습니다.블로그 투고에서 어셈블리 구성 xml을 사용하면 종속성이 확장되거나 폴더에 수집되어 매니페스트의 클래스 경로 엔트리에 의해 참조되는 경우에도 제어할 수 있습니다.
이상적인 솔루션은 lib 폴더에 jars를 포함시키는 것이며 main jar의 manifest.mf 파일에는 클래스 경로의 모든 jars가 포함됩니다.
바로 여기에 설명이 있습니다.https://caffebig.wordpress.com/2013/04/05/executable-jar-file-with-dependent-jars-using-maven/
제 경험이 누군가에게 도움이 되었으면 합니다.앱 Spring(cas 클라이언트 사용)을 Spring Boot(아직 2.4가 아닌 1.5)로 이행하고 싶습니다.나는 다음과 같은 많은 문제를 안고 있었다.
target/cas-client-web.jar에 메인 매니페스트 속성이 없습니다.
나는 모든 의존성을 가진 하나의 독특한 항아리를 만들고 싶다.며칠 동안 인터넷을 검색한 후.다음 행을 사용하여 작업을 수행할 수 있습니다.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<mainClass>${start-class}</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${start-class}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
start-class가 메인 클래스입니다.
<properties>
<java.version>1.8</java.version>
<start-class>com.test.Application</start-class>
</properties>
그리고 나의 어플리케이션은 다음과 같습니다.
package com.test;
import java.util.Arrays;
import com.test.TestProperties;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableAutoConfiguration
@EnableConfigurationProperties({TestProperties.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}
언급URL : https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven
'programing' 카테고리의 다른 글
vuex의 상태를 갱신하려면 어떻게 해야 합니까?vue.js 2 (0) | 2022.08.08 |
---|---|
두 div 요소 간의 vue2 전이 (0) | 2022.08.08 |
혼동: es6 브라우저 모듈을 Typescript와 함께 사용하는 방법(모듈 번들러 없음) (0) | 2022.08.08 |
Vue v2.5.17에서 Vue-mapbox가 맵을 로드하지 않음 (0) | 2022.08.08 |
스프링 부트 - 데이터베이스 유형 NONE에 대한 내장형 데이터베이스 드라이버 클래스를 확인할 수 없습니다. (0) | 2022.08.07 |