无敌萌萌馨

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

编译Java源码,一般只需设置编译的jdk版本

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-compiler-plugin</artifactId>  
    <configuration>  
        <source>1.8</source>  
        <target>1.8</target>  
        <encoding>UTF-8</encoding>  
    </configuration>  
</plugin> 

source:   源代码编译版本;   
target:   目标平台编译版本;   
encoding:   字符集编码。

 

自动打包源代码,用户可以根据需要将任何插件目标绑定到任何生命周期的阶段,

如:将maven-source-plugin的jar-no-fork目标绑定到default生命周期的package阶段,这样,以后在执行mvn package命令打包项目时,在package阶段之后会执行源代码打包

execution下可配置phase属性, 意思是在什么阶段打包源文件。如<phase>install</phase>:在执行mvn install时打包源代码

<plugin>  
          <groupId>org.apache.maven.plugins</groupId>  
         <artifactId>maven-source-plugin</artifactId>  
          <version>2.1.1</version>  
          <executions>  
              <execution>  
                  <id>attach-sources</id>  
                  <phase>package</phase>
                  <goals>  
                     <goal>jar-no-fork</goal>  
                 </goals>  
             </execution>  
         </executions>  
</plugin> 

   

Maven项目使用flatten-maven-plugin对发布的POM进行精简

使用maven开发的模块化应用,各种项目都具有多模块设置,但是通常希望同一项目的模块之间的依赖关系使用最新版本.使用flatten-maven-plugin,您可以在统一的一个地方定义版本,如parent/pom.xml 中。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>flatten-maven-plugin</artifactId>
    <version>1.2.2</version>
    <configuration>
        <updatePomFile>true</updatePomFile>
        <flattenMode>resolveCiFriendliesOnly</flattenMode>
    </configuration>
    <executions>
        <execution>
            <id>flatten</id>
            <phase>process-resources</phase>
            <goals>
                <goal>flatten</goal>
            </goals>
        </execution>
        <execution>
            <id>flatten.clean</id>
            <phase>clean</phase>
            <goals>
                <goal>clean</goal>
            </goals>
        </execution>
    </executions>
</plugin>

这个插件的作用是,生成一个压缩版的pom.xml文件,然后在installdeploy阶段使用压缩后的pom.xml文件,替换原来的pom.xml文件,具体压缩策略如下:

  • 和构建有关的元素会被删除;
  • 和开发有关的元素默认会被删除;
  • 只包含构件的使用者必须的一些信息;
  • 变量会被解析;
  • 上级关系会被解析,然后被压缩删除;
  • 构建时实际使用的profile会被评估,视情况处理;
  • 由JDK或者OS驱动的profile会被保留,需要时可以动态地控制依赖。

在默认的压缩逻辑下,插件如何处理各种元素,可以看这里。 下面会重点介绍如何通过各种参数来控制压缩的过程:

属性名类型描述
embedBuildProfileDependencies Boolean 由OS或者JDK的不同而触发的profile,可能根据环境的不同而产生不同的依赖,但是由属性等触发的profile,就不确定了,如果属性设置为true,profile中的依赖会直接写入生成的pom中,如果设置为false,所有的profile信息都会保留,默认是false
flattenMode FlattenMode 插件预定义了若干种压缩模式,下面会详述。
flattenedPomFilename String 生成的压缩后的pom.xml文件的文件名,默认为.flattened-pom.xml
outputDirectory File 生成的压缩后的pom.xml文件的存放位置,默认为${project.basedir}
pomElements FlattenDescriptor 该元素定义了如何处理额外的元素,如果可能,尽量使用flattenMode,这个元素仅仅用于进一步提高灵活性,它可以控制具体的某个元素是保留还是删除,比如要指定删除repositories,可以这样:<pomElements><repositories>flatten</repositories></pomElements>
updatePomFile Boolean 插件默认只会处理packaging属性为非pom的,如果要处理packagingpom的,可将本属性值设置为true

插件预定义了若干种模式,可以满足若干种常见的场景,这些模式定义在org.codehaus.mojo.flatten.FlattenMode枚举中,具体可以看代码,本文简单描述如下:

模式描述
minimum 不推荐使用,会展开pluginRepositories
bom 会保留dependencyManagement,展开properties
oss 推荐开源项目使用,会展开ciManagementcontributorsdistributionManagementinceptionYearissueManagementmailingListsorganizationprerequisites
ossrh 会展开namedescriptionurlscmdevelopers
defaults 会展开repositories
clean 删除全部可选元素

 

 

maven-deploy-plugin一般用于将项目发布到远程仓库
<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.8.2</version>
            <configuration>
                <skip>true</skip>  true标识跳过该模块
            </configuration>
        </plugin>
</plugins>

 

maven-resources-plugin用来处理资源文件。

 <build>    
<plugin>

<artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <executions> <execution> <id>copy-dto</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> -- copy资源 </goals> <configuration> <outputDirectory>${basedir}/src/main/java/dto</outputDirectory> --basedir:根路径,一般是当前项目名字。将资源复制到当前路径
<resources> <resource> <directory>../license-common/src/main/java/dto</directory> <includes> <include>**/*.java</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> </build>

 

 

Maven Enforcer plugin

Enforcer插件提供了控制某些环境约束的目标,例如Maven版本、JDK版本和操作系统系列,以及更多的内置规则和用户创建的规则。

 <plugin>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>1.4.1</version>
                <executions>
                    <execution>
                        <id>enforce-rules</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <phase>validate</phase>
                        <configuration>
                            <rules>
                                <requireJavaVersion>
                                    <message>
                                        <![CDATA[You are running an older version of Java. This application requires at least JDK ${java.version}.]]>
                                    </message>
                                    <version>[1.8,)</version>  --要求java版本1.8以上
                                </requireJavaVersion>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

 

posted on 2021-08-31 16:57  无敌萌萌馨  阅读(662)  评论(0)    收藏  举报