maven常用插件整理

maven插件内置信息

maven自带插件
  • mvn clean 清理
  • mvn compiler 编译
  • mvn deploy 发布
  • mvn install 构建
  • mvn verifier 校验
  • mvn test 测试
内置变量
  • ${basedir} 表示项目根目录,即包含pom.xml文件的目录;
  • ${version} 表示项目版本;
  • ${project.basedir} 同{basedir};
  • ${project.baseUri} 表示项目文件地址;
  • ${maven.build.timestamp} 表示项目构件开始时间;
  • ${maven.build.timestamp.format} 表示格式化时间 默认值为yyyyMMdd-HHmm
  • ${project.build.directory} 表示主源码路径;
  • ${project.build.sourceEncoding} 表示主源码的编码格式;
  • ${project.build.sourceDirectory} 表示主源码路径;
  • ${project.build.finalName} 表示输出文件名称;
  • ${project.version} 表示项目版本,同{version};
  • ${project.xxx} 当前pom文件的任意节点的内容
  • ${env.xxx} 获取系统环境变量。
  • ${settings.xxx} 指代了settings.xml中对应元素的值。

配置插件

Springboot插件

<plugin>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-maven-pluginartifactId>
	<configuration>
	    <mainClass>cui.yao.nan.erureka.EurekaServerApplicationmainClass>
	configuration>
plugin>
dokcer插件
  • 帮助我们在Maven工程中,通过简单的配置,自动生成镜像并推送到仓库中

  • 使用命令行

    • mvn clean package docker:build 只执行 build 操作
    • mvn clean package docker:build -DpushImage 执行 build 完成后 push 镜像
    • mvn clean package docker:build -DpushImageTag 执行 build 并 push 指定 tag 的镜像
    • mvn package -DskipDockerBuild 跳过构建
    • mvn package -DskipDockerTag 跳过tag镜像
    • mvn package -DskipDockerPush 跳过push
    • mvn package -DskipDocker 跳过整个过程
  • 使用POM的方式

<plugin>
    <groupId>com.spotifygroupId>
    <artifactId>docker-maven-pluginartifactId>
    <version>1.0.0version>
    <configuration>
        <imageName>mavendemoimageName>
        <baseImage>javabaseImage>
        <maintainer>docker_maven [email protected]maintainer>
        <workdir>/ROOTworkdir>
        <cmd>["java", "-version"]cmd>
        <entryPoint>["java", "-jar", "${project.build.finalName}.jar"]entryPoint>
        
        <resources>
            <resource>
                <targetPath>/ROOTtargetPath>
                
                <directory>${project.build.directory}directory>
                
                <include>${project.build.finalName}.jarinclude>
            resource>
        resources>
    configuration>
plugin>
  • 使用dockerfile方式
 <plugin>
    <groupId>com.spotifygroupId>
    <artifactId>docker-maven-pluginartifactId>
    <version>1.0.0version>
    <configuration>
        <imageName>mavendemoimageName>
        <dockerDirectory>${basedir}/dockerdockerDirectory> 
        
        <resources>
            <resource>
                <targetPath>/ROOTtargetPath>
                <directory>${project.build.directory}directory>
                <include>${project.build.finalName}.jarinclude>
            resource>
        resources>
    configuration>
plugin>   
  • ${basedir}/docker/Dockerfile 配置
FROM java
MAINTAINER docker_maven [email protected]
WORKDIR /ROOT
CMD ["java", "-version"]
ENTRYPOINT ["java", "-jar", "${project.build.finalName}.jar"]
compiler-编译插件
  • 使用命令: mvn -compile
<plugin>                                                                                                                                                                                                  
    <groupId>org.apache.maven.pluginsgroupId>                                                                                               
    <artifactId>maven-compiler-pluginartifactId>                                                                                            
    <version>3.1version>
    <configuration>                                                                                                                                          
        <source>1.8source>                                                                                              
        <target>1.8target>                                                                                      
        <encoding>UTF-8encoding>
        <skipTests>trueskipTests>
        <meminitial>128mmeminitial>
        <maxmem>512mmaxmem>                                                                                          
    configuration>                                                                                                                          
plugin>
jar-打包插件
  • 使用命令 mvn -package

<plugin>
    <groupId>org.apache.maven.pluginsgroupId>
    <artifactId>maven-jar-pluginartifactId>
    <configuration>
        <classesDirectory>target/classes/classesDirectory>
        <archive>
            <manifest>
                
                <mainClass>com.alibaba.dubbo.container.MainmainClass>
                
                
                <useUniqueVersions>falseuseUniqueVersions>
                <addClasspath>trueaddClasspath>
                <classpathPrefix>lib/classpathPrefix>
            manifest>
            <manifestEntries>
                <Class-Path>.Class-Path>
            manifestEntries>
        archive>
        
        <excludes>
            <exclude>**/profiles/**exclude>
            <exclude>**/jdbc.propertiesexclude>
            <exclude>**/*.protoexclude>
        excludes>
    configuration>
plugin>
source-打包源码插件
  • 使用命令 mvn -package
<plugin>
    <groupId>org.apache.maven.pluginsgroupId>
    <artifactId>maven-source-pluginartifactId>
    <executions>
        <execution>
            <id>attach-sourcesid>
            <goals>
                <goal>jargoal>
            goals>
        execution>
    executions>
plugin>

<!--最好先指定过如下属性—>
<properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
...
properties>

shade-自动化打包jar文件

<plugin>
    <groupId>org.apache.maven.pluginsgroupId>
    <artifactId>maven-shade-pluginartifactId>
    <version>3.1.0version>
    <executions>
        <execution>
            <phase>packagephase>
            <goals>
                <goal>shadegoal>
            goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.handlersresource>
                    transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.schemasresource>
                    transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>org.chench.MainmainClass>
                    transformer>
                transformers>
            configuration>
        execution>
    executions>
plugin>
dependency处理依赖问题
  • 该功能已经作为idea的插件处理了,一般没什么用
  • 被依赖模块jar文件中class文件提取出来放在指定位置

<plugin>
    <groupId>org.apache.maven.pluginsgroupId>
    <artifactId>maven-dependency-pluginartifactId>
    <executions>
        <execution>
            <id>unpackid>
            <phase>prepare-packagephase>
            <goals>
                <goal>unpackgoal>
            goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>com.xxxgroupId>
                        <artifactId>xxx-xxxartifactId>
                        <version>1.0.0version>
                        <type>jartype>
                        <includes>**/*.classincludes>
                        <overWrite>falseoverWrite>
                        <outputDirectory>${project.build.directory}/classesoutputDirectory>
                    artifactItem>
                artifactItems>
            configuration>
        execution>
    executions>
plugin>
  • 将scope为system的依赖jar包一起打包

<plugin>
    <groupId>org.apache.maven.pluginsgroupId>
    <artifactId>maven-dependency-pluginartifactId>
    <version>3.0.2version>
        <executions>
            <execution>
                <phase>prepare-packagephase>
                <goals>
                    <goal>copy-dependenciesgoal>
                goals>
            execution>
        executions>
        <configuration>
            <includeScope>systemincludeScope>
            <outputDirectory>${project.build.directory}/classesoutputDirectory>
        configuration>
plugin>
  • 将scope为system的依赖jar包中的class文件解压出来重新打包

<plugin>
    <groupId>org.apache.maven.pluginsgroupId>
    <artifactId>maven-dependency-pluginartifactId>
    <version>3.0.2version>
    <executions>
        <execution>
            <id>unpackid>
            <phase>prepare-packagephase>
            <goals>
                <goal>unpack-dependenciesgoal>
            goals>
        execution>
    executions>
    <configuration>
        <includeScope>systemincludeScope>
        <outputDirectory>${project.build.directory}/classesoutputDirectory>
    configuration>
plugin>
resource-默认资源文件处理
  • 指定打包和排除配置资源文件处理
<resources>
    <resource>
        
        <directory>src/main/resourcesdirectory>
        
        <excludes>
            <exclude>**/*.svnexclude>
        excludes>
    resource>
    <resource>
        <directory>src/main/resources/profiles/${profile.dir}directory>
        <includes>
            <include>*.propertiesinclude>
        includes>
    resource>
resources>
javadoc-生成javaDoc文件
  • 为项目生成javadoc文件
  • 执行命令
    • mvn javadoc:javadoc
    • mvn javadoc:jar
    • mvn javadoc:aggregate
    • mvn javadoc:aggregate-jar
    • mvn javadoc:test-javadoc
    • mvn javadoc:test-jar
    • mvn javadoc:test-aggregate
    • mvn javadoc:test-aggregate-jar
<plugin>  
    <groupId>org.apache.maven.pluginsgroupId>  
    <artifactId>maven-javadoc-pluginartifactId>  
    <version>2.10.2version>  
    <configuration>  
        <aggregate>trueaggregate>  
    configuration>  
    <executions>  
        <execution>  
            <id>attach-javadocsid>  
            <goals>  
                <goal>jargoal>  
            goals>  
        execution>  
    executions>  
plugin> 

maven常用插件名称整理

<artifactId>maven-pmd-pluginartifactId>
<artifactId>maven-checkstyle-pluginartifactId>

你可能感兴趣的:(服务端运维技术整理,maven,java)