This is the fifth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

There is an important package stage in maven’s life cycle. For Java Web projects, you can use war packages in poM files. For Java projects, you can use JAR packages. A single JAR package will not work. Here are two ways to use Maven to package dependencies.

Package as a JAR package with dependencies

Using two Maven plug-ins, you can package dependencies and source code into JARS

assembly

Add the following configuration to the POM file plug-in section:

	<plugin>
	    <artifactId>maven-assembly-plugin</artifactId>
	    <configuration>
	        <archive>
	            <manifest>
	            	<! -- Java -jar xxx.jar -->
	                <mainClass>${exec.mainClass}</mainClass>
	            </manifest>
	        </archive>
	        <descriptorRefs>
	            <descriptorRef>jar-with-dependencies</descriptorRef>
	        </descriptorRefs>
	    </configuration>
	</plugin>
Copy the code

Run Maven’s Assembly plug-in to package your code into a jar package with dependencies. In the target folder, you can see that xxx. JAR is packaged for the Package phase. Xxx-jar-with-dependencies. Jar

is the official custom package. Bin, jar-with-dependencies, SRC, and project are not recommended for use in these dependencies. You are advised to use the custom packaging mode. The configuration is as follows:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-assembly-plugin</artifactId>
	<version>2.2 - beta - 4</version>
	<configuration>
		<descriptors>
			<descriptor>src/main/assembly/src.xml</descriptor>
		</descriptors>
		<archive>
			<manifest>
				<mainClass>${exec.mainClass}</mainClass>  
			</manifest>
		</archive>
	</configuration>
</plugin>
Copy the code

The packaging action is specified in the src.xml file:

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.1"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.1 http://maven.apache.org/xsd/assembly-1.1.1.xsd">
    <id>jar-with-dependencies</id>
    <formats>
        <format>jar</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <unpack>true</unpack>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
        	<! -- Resource file output -->
            <directory>src\main\resources</directory>
            <outputDirectory>\</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.build.outputDirectory}</directory>
        </fileSet>
    </fileSets>
</assembly>
Copy the code

shade

The Shade plug-in allows us to choose which dependencies to include and which to exclude for the generated JAR package.

  1. Two operations are supported: include and exclude
  2. Format: groupId:artifactId[[:type]:classifier], which contains at least groupId and artifactId. Type and class name are optional
  3. Support ‘*’ and ‘? ‘Performs wildcard matching

When the Shade plug-in is packaged, it is possible to combine the Spring. schemas files in all jars. In the resulting single JAR package, spring.schemas contain a collection of all the versions that have ever been generated

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <filters>
                    <filter>
                        <artifact>:</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
                <! -- Default is true. Note that this property will shrink your POM file if you deploy using this plugin, or publish to a central repository, and will kill dependencies -->
                <createDependencyReducedPom>false</createDependencyReducedPom>
                <transformers>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <! -- This is your program entry file -->
                        <mainClass>com.timer.CollectBuptBBS</mainClass>
                    </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>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

Copy the code

Execute MVN package to generate two JAR files, one is the original original- xxx-1.0-snapshot. jar and the other is the executable xxx-1.0-snapshot. jar.

Package as external dependencies

Perform the following configuration in the POM file:

<! When you package jar files, configure the manifest file and add jar dependencies to the lib package -->
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<configuration>
		<archive>
			<manifest>
				<addClasspath>true</addClasspath>
				<classpathPrefix>lib/</classpathPrefix>
				<mainClass>com.timer.CollectBuptBBS</mainClass>
			</manifest>
		</archive>
	</configuration>
</plugin>
 <! -- copy the dependent JAR package to the lib directory -->
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
	<executions>
		<execution>
			<id>copy</id>
			<phase>package</phase>
			<goals>
				<goal>copy-dependencies</goal>
			</goals>
			<configuration>
				<outputDirectory>
					${project.build.directory}/lib
				</outputDirectory>
			</configuration>
		</execution>
	</executions>
</plugin>
Copy the code

The jar file and the lib folder are generated in the target folder. The JAR file is the source package. The internal path of the main class and dependency is the lib folder, and the lib folder is the dependent JAR package. In the runtime environment, you need to put the lib and JAR files in the same path and run the Java -r xxx.jar command.