Using maven compiler plugin and maven shade plugin to complete maven project packaging

Keywords: Java Maven Apache xml

Recently, I was in charge of a pure maven project (project requirements should be as light as possible), and I need to complete the packaging work by myself.
Therefore, based on Maven compiler plugin and Maven shade plugin, the project packaging is completed.

Among them:

  • Maven compiler plugin is responsible for project compilation;
  • maven-shade-plugin is responsible for the final packaging operation.

The following operations are performed in pom.xml file.

Project basic properties

    <groupId>com.test</groupId>
    <artifactId>app</artifactId> //
    <version>0.1.0</version>
    <packaging>jar</packaging>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

Add plug-ins

<build>
        <plugins>

            <!-- Compile java project-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>

            <!-- Create a fat jar,Include all necessary dependencies -->
            <!-- According to their own main Function replacement<mainClass>...</mainClass>Configuration in -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <!-- Run shade goal on package phase -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <artifactSet>
                                <!-- Specify files that do not need to be packaged(You can use wildcards) -->
                                <excludes>
                                    <exclude>org.apache.flink:force-shading</exclude>
                                    <exclude>com.google.code.findbugs:jsr305</exclude>
                                    <exclude>org.slf4j:*</exclude>
                                    <exclude>log4j:*</exclude>
                                </excludes>
                            </artifactSet>
                            <filters>
                                <filter>
                                    <!-- Do not copy the signatures in the META-INF folder.
                                    Otherwise, this might cause SecurityExceptions when using the JAR. -->
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.test.app.Application</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

Tips:

maven-shade-plugin is more powerful, and it can also solve the dependency conflicts in package files. Readers can find relevant articles by themselves.

PS:
If you think my article is helpful to you, please pay attention to my wechat public account, thank you!

Posted by cookiemonster4470 on Fri, 18 Oct 2019 13:15:26 -0700