programing tip

메이븐을 사용하여 뚱뚱한 항아리 만들기

itbloger 2020. 8. 7. 08:06
반응형

메이븐을 사용하여 뚱뚱한 항아리 만들기


항아리로 배포하려는 코드 기반이 있습니다. 또한 최종 항아리에 번들로 묶고 싶은 외부 항아리에 대한 종속성이 있습니다.

을 사용하여이 작업을 수행 할 수 있다고 들었지만 maven-assembly-plug-in방법을 모르겠습니다. 누군가 나를 몇 가지 예를 들어 줄 수 있습니까?

지금은 마지막 병을 묶기 위해 지방 병을 사용하고 있습니다. maven을 사용하여 동일한 것을 달성하고 싶습니다.


참고 : 스프링 부트 애플리케이션 인 경우 답변 끝을 읽으십시오.

다음 플러그인을 추가하십시오 pom.xml. 최신 버전은 https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-assembly-plugin 에서 찾을 수 있습니다.

...
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>CHOOSE LATEST VERSION HERE</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>assemble-all</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
...

이 플러그인을 구성한 후 실행 mvn package하면 두 개의 jar가 생성됩니다. 하나는 프로젝트 클래스 만 포함하고 다른 하나는 "-jar-with-dependencies"접미사가있는 모든 종속성이있는 두 번째 fat jar입니다.

classpath런타임에 올바른 설정 을 원하면 다음 플러그인도 추가하십시오.

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

maven-shade-plugin을 사용할 수 있습니다 .

빌드에서 shade 플러그인을 구성한 후 명령 mvn package은 모든 종속성이 병합 된 단일 jar를 생성합니다.


아마도 당신은 maven-shade-plugin충돌을 피하기 위해 종속성을 번들로 묶고 사용하지 않는 코드를 최소화하고 외부 종속성을 숨길 수 있습니다.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <minimizeJar>true</minimizeJar>
                        <createDependencyReducedPom>true</createDependencyReducedPom>
                        <dependencyReducedPomLocation>
                            ${java.io.tmpdir}/dependency-reduced-pom.xml
                        </dependencyReducedPomLocation>
                        <relocations>
                            <relocation>
                                <pattern>com.acme.coyote</pattern>
                                <shadedPattern>hidden.coyote</shadedPattern>
                            </relocation>
                        </relocations>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

참조 :


actually, adding the

<archive>
   <manifest>
    <addClasspath>true</addClasspath>
    <packageName>com.some.pkg</packageName>                     
    <mainClass>com.MainClass</mainClass>
  </manifest>
</archive>

declaration to maven-jar-plugin does not add the main class entry to the manifest file for me. I had to add it to the maven-assembly-plugin in order to get that in the manifest


You can use the onejar-maven-plugin for packaging. Basically, it assembles your project and its dependencies in as one jar, including not just your project jar file, but also all external dependencies as a "jar of jars", e.g.

<build>
    <plugins>
        <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>
    </plugins>
</build>

Note 1: Configuration options is available at the project home page.

Note 2: For one reason or the other, the onejar-maven-plugin project is not published at Maven Central. However jolira.com tracks the original project and publishes it to with the groupId com.jolira.


An alternative is to use the maven shade plugin to build an uber-jar.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version> Your Version Here </version>
    <configuration>
            <!-- put your configurations here -->
    </configuration>
    <executions>
            <execution>
                    <phase>package</phase>
                    <goals>
                            <goal>shade</goal>
                    </goals>
            </execution>
    </executions>
</plugin>

참고URL : https://stackoverflow.com/questions/16222748/building-a-fat-jar-using-maven

반응형