常用MAVEN打包方式总结

几种常用的maven打包插件总结:
一、自带插件:
maven自带的核心插件为Build plugins和Reporting plugins。
mvn compile编译源码实际上就利用到了maven-compiler-plugin,其他phase也类似用到了相应的插件
关于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.pluginsgroupId>  
  <artifactId>maven-compiler-pluginartifactId>  
  <version>3.6.1version>  
  <configuration> 
    <encoding>utf-8encoding>  
    <source>1.7source>  
      
    <target>1.7target>  
      
    <compilerArgs> 
        
      <arg>-verbosearg>  
      <arg>-Xlint:all,-options,-patharg> 
    compilerArgs>  
    <fork>truefork>  
      
    <meminitial>128mmeminitial>  
      
    <maxmem>512mmaxmem>  
      
    <executable>${JAVA_1_4_HOME}/bin/javacexecutable>  
      
    <compilerVersion>1.3compilerVersion>  
     
  configuration> 
plugin>

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


    .compiler.source>1.8.compiler.source>
    .compiler.target>1.8.compiler.target>
  

二、直接打成可执行的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-pluginartifactId>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependenciesdescriptorRef>
      descriptorRefs>
    configuration>
plugin>

还一种方式,可以使用spring-boot的打包插件进行打包。一般是跟spring boot一起使用的,但是也可以单独利用出来打成可执行的整包。

<plugin> 
  <groupId>org.springframework.bootgroupId>  
  <artifactId>spring-boot-maven-pluginartifactId>  
  <version>1.3.5.RELEASEversion>  
  <configuration>
    <mainClass>com.ftoul.dataCleaner.DataCleanServiceProvidermainClass>  
    <layout>ZIPlayout> 
  configuration>  
  <executions> 
    <execution> 
      <goals> 
        <goal>repackagegoal> 
      goals> 
    execution> 
  executions> 
plugin>

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

<plugin>
  <groupId>org.apache.maven.pluginsgroupId>
  <artifactId>maven-shade-pluginartifactId>
  <version>3.0.0version>
  <executions>
    <execution>
      <phase>packagephase>
      <goals>
        <goal>shadegoal>
      goals>
      <configuration>
        <transformers>
          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <manifestEntries>
              <Main-Class>org.sonatype.haven.ExodusCliMain-Class>
              <Build-Number>123Build-Number>
            manifestEntries>
          transformer>
        transformers>
      configuration>
    execution>
  executions>
plugin>

三 将依赖包与代码包分开打包
这种打包方式更普遍适用。毕竟依赖的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!

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

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.pluginsgroupId>
      <artifactId>maven-dependency-pluginartifactId>
      <version>3.0.0version>
      <executions>
        <execution>
          <id>src-dependenciesid>
          <phase>packagephase>
          <goals>
            
            <goal>unpack-dependenciesgoal>
          goals>
          <configuration>
            <classifier>sourcesclassifier>
            <failOnMissingClassifierArtifact>falsefailOnMissingClassifierArtifact>
            <outputDirectory>${project.build.directory}/sourcesoutputDirectory>
          configuration>
        execution>
      executions>
    plugin>
  plugins>
build>

将指定的包打入依赖包

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.pluginsgroupId>
      <artifactId>maven-dependency-pluginartifactId>
      <version>3.0.0version>
      <executions>
        <execution>
          <id>copyid>
          <phase>packagephase>
          <goals>
            <goal>copygoal>
          goals>
        execution>
      executions>
      <configuration>
        <artifactItems>
          <artifactItem>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.1version>
            <type>jartype>
            <overWrite>falseoverWrite>
            <outputDirectory>${project.build.directory}/alternateLocationoutputDirectory>
            <destFileName>optional-new-name.jardestFileName>
          artifactItem>
        artifactItems>
        <outputDirectory>${project.build.directory}/warsoutputDirectory>
        <overWriteReleases>falseoverWriteReleases>
        <overWriteSnapshots>trueoverWriteSnapshots>
      configuration>
    plugin>
  plugins>
build>

一个可用的示例

<plugin>
    <groupId>org.apache.maven.pluginsgroupId>
    <artifactId>maven-dependency-pluginartifactId>
    
    <executions>
        <execution>
            <id>copy-dependenciesid>
            <phase>packagephase>
            <goals>
                <goal>copy-dependenciesgoal>
            goals>
            <configuration>
                <outputDirectory>exportoutputDirectory> 
                <excludeTransitive>falseexcludeTransitive>
                <stripVersion>truestripVersion>
            configuration>
        execution>
    executions>
plugin>

四、maven-jar-plugin 将指定的一些文件打包成jar包
这个比较简单。就将指定的文件打成jar包

<plugin> 
  <groupId>org.apache.maven.pluginsgroupId>  
  <artifactId>maven-jar-pluginartifactId>  
  <version>3.0.2version>  
  <configuration>  
    <archive> 
      <manifest> 
        <addClasspath>trueaddClasspath>  
        <classpathPrefix>support/classpathPrefix>  
        <mainClass>com.myapp.MyAppApplicationmainClass> 

        <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MFmanifestFile>
      manifest> 
    archive>  
  configuration>
  <executions> 
    <execution> 
      <id>myapp1-jarid>  
      <phase>packagephase>  
      <goals> 
        <goal>jargoal> 
      goals>  
      <configuration> 
        <classifier>myappclassifier>  
        <includes> 
          <include>com/myapp/**include>  
          <include>mybatis/**include>  
          <include>templates/**include>  
          <include>*.propertiesinclude>  
          <include>dubbo.xmlinclude> 
        includes> 
      configuration> 
    execution>  
    <execution> 
      <id>myapp2-jarid>  
      <phase>packagephase>  
      <goals> 
        <goal>jargoal> 
      goals>  
      <configuration> 
        <classifier>myapp2classifier>  
        <includes> 
          <include>com/myapp2/crawler/*include>  
          <include>com/myapp2/crawler/*include>  
          <include>com/myapp2/utils/**include>  
          <include>log4j.propertiesinclude> 
        includes> 
      configuration> 
    execution>  
  executions> 
plugin>

五、其他丰富的三方插件
PMD打包插件。 PMD一个很好用的静态代码检查插件, eclipse可以直接安装插件使用
生成PMD报告
http://maven.apache.org/plugins/maven-pmd-plugin/
只能用于3.3.3以后的maven版本

分析JSP页面

<reporting>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.pluginsgroupId>
      <artifactId>maven-pmd-pluginartifactId>
      <version>3.8version>
      <configuration>
        <language>jsplanguage>
        <rulesets>
          <ruleset>jsp-basicruleset>
        rulesets>
        <includes>
          <include>**/*.jspinclude>
        includes>
        <compileSourceRoots>
          <compileSourceRoot>${basedir}/src/main/webappcompileSourceRoot>
        compileSourceRoots>
      configuration>
    plugin>
  plugins>
reporting>

分析JS

<reporting>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.pluginsgroupId>
      <artifactId>maven-pmd-pluginartifactId>
      <version>3.8version>
      <configuration>
        <language>javascriptlanguage>
        <rulesets>
          <ruleset>ecmascript-basicruleset>
          <ruleset>ecmascript-bracesruleset>
          <ruleset>ecmascript-unnecessaryruleset>
        rulesets>
        <includes>
          <include>**/*.jsinclude>
        includes>
        <compileSourceRoots>
          <compileSourceRoot>${basedir}/src/main/javascriptcompileSourceRoot>
        compileSourceRoots>
      configuration>
    plugin>
  plugins>
reporting>

代码非法检查

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.pluginsgroupId>
      <artifactId>maven-pmd-pluginartifactId>
      <version>3.8version>
      <executions>
        <execution>
          <goals>
            <goal>checkgoal>
            <goal>cpd-checkgoal>
          goals>
        execution>
      executions>
    plugin>
  plugins>
build>

制定JDK

<reporting>
  <plugins>
    <plugin>
      <artifactId>maven-pmd-pluginartifactId>
      <version>3.8version>
      <configuration>
        <targetJdk>1.6targetJdk>
      configuration>
    plugin>
  plugins>
reporting>

删除PMD报告

<reporting>
  <plugins>
    <plugin>
      <artifactId>maven-pmd-pluginartifactId>
      <version>3.8version>
      <reportSets>
        <reportSet>
          <reports>
            <report>pmdreport>
            <report>cpdreport>
          reports>
        reportSet>
      reportSets>
    plugin>
  plugins>
reporting>

没事可以经常去官网转转,时不时有些新的打包插件出来。 比如PDF插件 GPG签名插件 TOMCAT插件 JETTY插件 等。 好多好多。用时慢慢去了解。

你可能感兴趣的:(maven)