一、自带插件:

maven自带的核心插件为Build plugins和Reporting plugins。

mvn compile编译源码实际上就利用到了maven-compiler-plugin,其他phase也类似用到了相应的插件

maven项目target无jsp常用MAVEN打包方法总结 Ruby

关于maven自带的核心插件见:http://maven.apache.org/plugins/index.html

核心插件 maven-compiler-plugin

参考地址 http://maven.apache.org/plugins/maven-compiler-plugin/

从3.0版本开始,编译工具默认利用的是 javax.tools.JavaCompiler(从JDK 1.6开始) 如果要逼迫利用javac来进行编译,须要添加参数forceJavacCompilerUse

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

<!-- 默认1.5 -->

<target>1.7</target>

<!-- 默认1.5 -->

<compilerArgs>

<!-- 通报编译参数 -->

<arg>-verbose</arg>

<arg>-Xlint:all,-options,-path</arg>

</compilerArgs>

<fork>true</fork>

<!-- 配置编译内存参数 -->

<meminitial>128m</meminitial>

<!-- 初始内存 -->

<maxmem>512m</maxmem>

<!-- 最大内存 -->

<executable>${JAVA_1_4_HOME}/bin/javac</executable>

<!-- 指定编译的java库 -->

<compilerVersion>1.3</compilerVersion>

<!-- 指定编译版本 -->

</configuration>

</plugin>

123456789101112131415161718192021222324252627

配置source 和target时,也可以这样配置 实际上这是javac指令可以接管的参数

<properties>

<maven.compiler.source>1.8</maven.compiler.source>

<maven.compiler.target>1.8</maven.compiler.target>

</properties>

1234

二、直接打成可实行的jar包。

这种打包办法比较粗暴,将运用的依赖包和程序包打成一个全量包。
包会相对较大,但是好处也很明显,不用管依赖包。
以是这种办法只适用于比较小的项目,比如搭建微做事这种办法可以适用。

附上关键的做事器实行代码

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

解释: 末了面的& 表明后台实行。

1> 将运行日志输出到tracefile

2> 将缺点日志输出到errorfile

详细参数很多,参考官网页面

官网页面 http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

<plugin>

<artifactId>maven-assembly-plugin</artifactId>

<configuration>

<descriptorRefs>

<descriptorRef>jar-with-dependencies</descriptorRef>

</descriptorRefs>

</configuration>

</plugin>

12345678

还一种办法,可以利用spring-boot的打包插件进行打包。
一样平常是跟spring boot一起利用的,但是也可以单独利用出来打成可实行的整包。

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

12345678910111213141516

补充一下,还有一个shade插件也是比较常用的打fat jar包的办法

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-shade-plugin</artifactId>

<version>3.0.0</version>

<executions>

<execution>

<phase>package</phase>

<goals>

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

1234567891011121314151617181920212223

三 将依赖包与代码包分开打包

这种打包办法更普遍适用。
毕竟依赖的jar包在项目发展中变动会相对较小。
一样平常可合营maven-dependency-plugin 和 maven-jar-plugin 两个插件打包。
前者卖力依赖包,后者卖力程序包。

其余,附上做事器可用的实行脚本。

more runapp.sh

#!/bin/sh

#实行jar包

RUN_LIBS=\公众\公众

#依赖jar包

SUPPORT_LIBS=\公众\"大众

RUN_LIB_PATH=\"大众$HOME/app/lib\公众

SUPPORT_LIB_PATH=\"大众$HOME/app/support\"大众

#加载程序包

for i in ${RUN_LIB_PATH}/ ; do

RUN_LIBS=${RUN_LIBS}:$i

done

#加载依赖包

for i in ${SUPPORT_LIB_PATH}/ ; do

SUPPORT_LIBS=${SUPPORT_LIBS}:$i

done

#整合classpath

CLASSPATH=${RUN_LIBS}:${SUPPORT_LIBS}

export CLASSPATH

#调用java指令实行。
-D输入参数 java中可以用 System.getProperties读取。
同时指定实行入口类 SpringBootApplication 这是一个范例的Springboot的实行办法。

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!

123456789101112131415161718192021222324

assembly官网的其他几个示例利用项目的依赖包进行打包

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-dependency-plugin</artifactId>

<version>3.0.0</version>

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

123456789101112131415161718192021222324

将指定的包打入依赖包

<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>copy</goal>

</goals>

</execution>

</executions>

<configuration>

<artifactItems>

<artifactItem>

<groupId>junit</groupId>

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

12345678910111213141516171819202122232425262728293031323334

一个可用的示例

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-dependency-plugin</artifactId>

<!-- <version>2.10</version> -->

<executions>

<execution>

<id>copy-dependencies</id>

<phase>package</phase>

<goals>

<goal>copy-dependencies</goal>

</goals>

<configuration>

<outputDirectory>export</outputDirectory> <!-- 将依赖包放入export文件夹 -->

<excludeTransitive>false</excludeTransitive>

<stripVersion>true</stripVersion>

</configuration>

</execution>

</executions>

</plugin>

12345678910111213141516171819

四、maven-jar-plugin 将指定的一些文件打包成jar包

这个比较大略。
就将指定的文件打成jar包

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-jar-plugin</artifactId>

<version>3.0.2</version>

<configuration> <!-- manifest配置信息 紧张是可以配置主实行类。
有主实行类,可以用java-jar直接实行。
没有的话就须要指定实行类 -->

<archive>

<manifest>

<addClasspath>true</addClasspath>

<classpathPrefix>support/</classpathPrefix>

<mainClass>com.myapp.MyAppApplication</mainClass>

<!-- 可以按上面的办法自己配置,也可以指定MF文件打包。
-->

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051

五、其他丰富的三方插件

PMD打包插件。
PMD一个很好用的静态代码检讨插件, eclipse可以直接安装插件利用

天生PMD报告

http://maven.apache.org/plugins/maven-pmd-plugin/

只能用于3.3.3往后的maven版本

剖析JSP页面

<reporting>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-pmd-plugin</artifactId>

<version>3.8</version>

<configuration>

<language>jsp</language>

<rulesets>

<ruleset>jsp-basic</ruleset>

</rulesets>

<includes>

<include>/.jsp</include>

</includes>

<compileSourceRoots>

<compileSourceRoot>${basedir}/src/main/webapp</compileSourceRoot>

</compileSourceRoots>

</configuration>

</plugin>

</plugins>

</reporting>

123456789101112131415161718192021

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

1234567891011121314151617181920212223

代码造孽检讨

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-pmd-plugin</artifactId>

<version>3.8</version>

<executions>

<execution>

<goals>

<goal>check</goal>

<goal>cpd-check</goal>

</goals>

</execution>

</executions>

</plugin>

</plugins>

</build>

1234567891011121314151617

制订JDK

<reporting>

<plugins>

<plugin>

<artifactId>maven-pmd-plugin</artifactId>

<version>3.8</version>

<configuration>

<targetJdk>1.6</targetJdk>

</configuration>

</plugin>

</plugins>

</reporting>

1234567891011

删除PMD报告

<reporting>

<plugins>

<plugin>

<artifactId>maven-pmd-plugin</artifactId>

<version>3.8</version>

<reportSets>

<reportSet>

<reports>

<report>pmd</report>

<report>cpd</report>

</reports>

</reportSet>

</reportSets>

</plugin>

</plugins>

</reporting>

12345678910111213141516

没事可以常常辞官网转转,时时时有些新的打包插件出来。
比如PDF插件 GPG署名插件 TOMCAT插件 JETTY插件 等。
好多好多。
用时逐步去理解。