Maven is used every day, but do you really understand the packaging methods of Maven? Here is a summary of several common Maven packaging plugins:

One, built-in plug-in:

Maven’s core plugins are Build plugins and Reporting plugins. MVN compile compile the source code is actually using the maven – compiler – the plugin, other phase are similar to the corresponding plug-in See on maven’s own core plugins: maven.apache.org/plugins/ind…

Core plugin maven – compiler – maven.apache.org/plugins/mav plugin reference address… As of 3.0, the default compilation tool is javax.tools.javacompiler (as of JDK 1.6). To force javac to compile, you need to add the forceJavacCompilerUse parameter

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <encoding> UTF-8 </encoding> <source>1.7</source> <! <target>1.7</target> <! -- defaults to 1.5 --> <compilerArgs> <! -- pass compilation parameters --> <arg>-verbose</arg> <arg> -xlint :all,-options,-path</arg> </compilerArgs> <fork>true</fork> <! < span style = "box-sizing: border-box; color: RGB (74, 74, 74); line-height: 22px; white-space: inherit! Important;" -- initial memory --> <maxmem> 512MB </maxmem> <! - the maximum amount of memory - > < the executable > ${JAVA_1_4_HOME} / bin/javac < / the executable > <! </compilerVersion> <! </plugin> </plugin>Copy the code

When you configure the source and the target, you can also configure it this way and in fact this is an acceptable parameter to the JavAC directive

Piler < properties > < maven.com. Source > 1.8 < / maven.com piler source > < maven.com piler. Target > 1.8 < / maven.com piler. Target > </properties>Copy the code

2, directly into the executable JAR package.

This is a rough way to package an application’s dependencies and program packages into one full package. Packages are relatively large, but the benefits are obvious, not to worry about dependency packages. So this approach is only suitable for small projects, like building microservices.

Attach key server execution code

java -jar dataCleaner.jar 1>/home/credit/app/tracefile 2>/home/credit/app/errorfile &
Copy the code

Description: The last & indicates background execution. 1> Output run logs to tracefile. 2> Output error logs to errorFile

Many specific parameters, refer to the website page website page maven.apache.org/plugins/mav…

<plugin>
	<artifactId>maven-assembly-plugin</artifactId>
	<configuration>
	  <descriptorRefs>
		<descriptorRef>jar-with-dependencies</descriptorRef>
	  </descriptorRefs>
	</configuration>
</plugin>
Copy the code

Alternatively, you can use spring-boot’s packaging plug-in for packaging. It is typically used in conjunction with Spring Boot, but can also be used separately as an executable package.

<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> < version > 1.3.5. RELEASE < / version > < configuration > < mainClass > com. Ftoul. DataCleaner. DataCleanServiceProvider < / mainClass > <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>Copy the code

In addition, there is also a shade plugin which is a common way to use fat JAR packages

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.0.0</version> <execution> <phase>package</phase> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>org.sonatype.haven.ExodusCli</Main-Class> <Build-Number>123</Build-Number> </manifestEntries> </transformer>  </transformers> </configuration> </execution> </executions> </plugin>Copy the code

Package dependency packages separately from code packages

This type of packaging is more common. After all, the dependent JAR packages will change relatively little over the course of the project. It is packaged with maven-dependency- Plugin and Maven-jar-plugin. The former takes care of dependency packages, while the latter takes care of packages.

In addition, attach execution scripts available to the server.

more runapp.sh #! $HOME/app/ lib_path ="$HOME/app/support" ${RUN_LIB_PATH}/*; Do RUN_LIBS=${RUN_LIBS}:$I done for I in ${SUPPORT_LIB_PATH}/*; Do SUPPORT_LIBS=${SUPPORT_LIBS}:$I done classpath =${RUN_LIBS}:${SUPPORT_LIBS} export classpath The -d input parameter Java can be read with system.getProperties. Also specify the entry class SpringBootApplication. This is a typical Springboot execution. java -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=27899,suspend=n -cp $CLASSPATH -Dspring.profiles.active=prod com.app.SpringBootApplication -D user.timezone=GMT+08 1>/home/credit/ftoulcloud/bin/tracefile 2>/home/credit/ftoulcloud/bin/errorfile & echo Start App Success!Copy the code

Several other examples on the Assembly website use project dependencies for packaging

<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> Executions > <execution> <id> SRC -dependencies</id> <phase>package</phase> <goals> <! -- use copy-dependencies instead if you don't want to explode the sources --> <goal>unpack-dependencies</goal> </goals> <configuration> <classifier>sources</classifier> <failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact> <outputDirectory>${project.build.directory}/sources</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build>Copy the code

Puts the specified package into the dependency package

<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>copy</id> <phase>package</phase> <goals> </goal> </goals> </execution> </executions> <configuration> <artifactItems> <artifactItem> <groupId>junit</groupId> The < artifactId > junit < / artifactId > < version > 3.8.1 < / version > < type > jar < / type > < overWrite > false < / overWrite > <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory> <destFileName>optional-new-name.jar</destFileName> </artifactItem> </artifactItems> <outputDirectory>${project.build.directory}/wars</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>true</overWriteSnapshots> </configuration> </plugin> </plugins> </build>Copy the code

An example available

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <! Executions > <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>export</outputDirectory> <! </stripVersion> </stripVersion> </stripVersion> </configuration> </execution> </executions> </plugin>Copy the code

Fourth, maven – jar – the plugin

It is easier to package some of the specified files as jars. Type the specified file into a JAR package

< plugin > < groupId > org. Apache. Maven. Plugins < / groupId > < artifactId > maven - jar - plugin < / artifactId > < version > 3.0.2 < / version > <configuration> <! The manifest configuration information is mainly used to configure the main execution class. There is a main execution class, which can be executed directly with Java -jar. <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>support/</classpathPrefix> <mainClass>com.myapp.MyAppApplication</mainClass> <! -- Can be configured as above, or you can specify the MF file package. --> <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile> </manifest> </archive> </configuration> <executions> <execution> <id>myapp1-jar</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classifier>myapp</classifier> <includes> <include>com/myapp/**</include> <include>mybatis/**</include> <include>templates/**</include> <include>*.properties</include> <include>dubbo.xml</include> </includes> </configuration> </execution> <execution> <id>myapp2-jar</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classifier>myapp2</classifier> <includes> <include>com/myapp2/crawler/*</include> <include>com/myapp2/crawler/*</include> <include>com/myapp2/utils/**</include> <include>log4j.properties</include> </includes> </configuration> </execution> </executions> </plugin>Copy the code

Five, other rich three-party plug-ins

PMD packages plug-ins. PMD a useful code statically for the presence of plug-in, eclipse can be directly installed plug-ins using generated maven.apache.org/plugins/mav PMD report… Only available with Maven versions later than 3.3.3

Analyzing a JSP page

<reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pmd-plugin</artifactId> <ruleset> </ruleset> </ruleset> </rulesets> </rulesets> </rulesets> </ruleset> </rulesets> <includes> <include>**/*.jsp</include> </includes> <compileSourceRoots> <compileSourceRoot>${basedir}/src/main/webapp</compileSourceRoot> </compileSourceRoots> </configuration> </plugin> </plugins> </reporting>Copy the code

Analysis of JS

<reporting>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-pmd-plugin</artifactId>
      <version>3.8</version>
      <configuration>
        <language>javascript</language>
        <rulesets>
          <ruleset>ecmascript-basic</ruleset>
          <ruleset>ecmascript-braces</ruleset>
          <ruleset>ecmascript-unnecessary</ruleset>
        </rulesets>
        <includes>
          <include>**/*.js</include>
        </includes>
        <compileSourceRoots>
          <compileSourceRoot>${basedir}/src/main/javascript</compileSourceRoot>
        </compileSourceRoots>
      </configuration>
    </plugin>
  </plugins>
</reporting>
Copy the code

Code illegal check

<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pmd-plugin</artifactId> Executions > <execution> <goal> <goal> executions> </goal> </goal> </goal> </goal> </goal> </goal> </goal> </goal> </goal> </goal> </goal> </executions> </plugin> </plugins> </build>Copy the code

To formulate the JDK

</artifactId> <version>3.8</version> <configuration> </reporting> </reporting>Copy the code

The PMD report was deleted

Maven-pmd-plugin </artifactId> <version>3.8</version> <reportSet>  <reports> <report>pmd</report> <report>cpd</report> </reports> </reportSet> </reportSets> </plugin> </plugins> </reporting>Copy the code

Nothing can often go to the official website around, from time to time some new packaging plug-ins out. For example, PDF plug-in, GPG signature plug-in, TOMCAT plug-in, JETTY plug-in, etc. Lots of them. Time slowly to understand.

White whoring innocent, long live sharing. If you find it helpful, please support me.