首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >maven常用插件

maven常用插件

作者头像
编程随笔
发布2019-09-11 16:02:20
1.5K0
发布2019-09-11 16:02:20
举报

一. 自带核心插件

https://maven.apache.org/plugins/

二. 第三方插件

1.maven-assembly-plugin

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

(1)打包独立运行的jar文件
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <!-- 配置main方法入口程序 -->
                <mainClass>org.chench.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

特别注意: 如果在项目中使用了Spring框架,在通过maven-assembly-plugin打包成独立可执行的jar包后,在执行时可能报如下错误:

Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace 

这其实是maven-assembly-plugin的一个BUG,详见:https://issues.apache.org/jira/browse/MASSEMBLY-360 解决方案:使用maven-shade-plugin插件进行打包。

(2)打包压缩文件

maven-assembly-plugin插件除了可以打包项目为可独立运行的jar文件,还可以将项目打包为压缩文件。

<!-- 部署打包: 通过maven-assembly插件压缩为tar包进行发布 -->
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <attach>false</attach>
        <descriptors>
            <descriptor>assembly.xml</descriptor>
        </descriptors>
        <finalName>${project.name}-${project.version}</finalName>
        <outputDirectory>${basedir}/release</outputDirectory>
    </configuration>
    <executions>
        <execution>
            <id>assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

assembly.xml配置:

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>distribution</id>
    <formats>
        <!-- 压缩包格式 -->
        <format>tar.gz</format>
        <!-- <format>zip</format> -->
    </formats>
    
    <!-- 打包文件参数配置 -->
    <fileSets>
        <!-- 脚本 -->
        <fileSet>
            <directory>${basedir}/src/main/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <includes>
                <include>*.bat</include>
                <include>*.sh</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>
        
        <!-- sql文件 -->
        <fileSet>
            <directory>${basedir}/src/main/sql</directory>
            <outputDirectory>sql</outputDirectory>
            <includes>
                <include>*.sql</include>
            </includes>
        </fileSet>
        
        <!-- jdbc.properties, logback.xml -->
        <fileSet>
            <directory>${basedir}/src/main/resources/profiles/${profile.dir}</directory>
            <outputDirectory>conf</outputDirectory>
            <includes>
                <include>jdbc.properties</include>
                <include>logback.xml</include>
            </includes>
        </fileSet>
        
        <!-- INSTALL,README,change.log -->
        <fileSet>
            <directory>${basedir}</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>INSTALL</include>
                <include>README.md</include>
                <include>change.log</include>
            </includes>
        </fileSet>
        
        <!-- 日志目录 -->
        <fileSet>
            <directory>target</directory>
            <outputDirectory>logs</outputDirectory>
            <excludes>
                <exclude>**/*</exclude>
            </excludes>
        </fileSet>
        
        <!-- 临时目录 -->
        <fileSet>
            <directory>target</directory>
            <outputDirectory>temp</outputDirectory>
            <excludes>
                <exclude>**/*</exclude>
            </excludes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <excludes>
                <exclude>junit:junit</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
    
    <includeBaseDirectory>true</includeBaseDirectory>
    <baseDirectory>${project.name}-${project.version}</baseDirectory>
</assembly>

2. maven-shade-plugin

https://maven.apache.org/plugins/maven-shade-plugin/

打包可独立运行的jar文件
<!-- 打包可执行jar文件 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>org.chench.Main</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

对于使用了Spring框架的项目,如果在执行java -jar package.jar时报错:

Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace

原因:如果在多个jar包中存在同名文件,如:mybatis-spring.jar和spring-context.jar中都存在META-INF/spring.schemas,maven在执行打包时 可能会使用mybatis-spring.jar中的spring.schemas覆盖掉spring-context.jar中的spring.schemas。 显然,mybatis-spring.jar中的spring.schemas定义的命名空间与spring-context.jar中spring.schemas定义的命名空间是不同的。 这会导致在加载Spring容器时无法找到合适的命名空间! 为了解决这个问题,可以在maven-shade-plugin中通过Resource Transformers将所有的spring.schemas文件中的内容都合并到一起。

<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
    <resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
    <resource>META-INF/spring.schemas</resource>
</transformer>

完整配置:

<!-- 打包可执行jar文件 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.handlers</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.schemas</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>org.chench.Main</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

详见:https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#AppendingTransformer

3. maven-dependency-plugin

https://maven.apache.org/plugins/maven-dependency-plugin/ 通过该插件可以对被依赖组件进行复制,解压等一系列操作。

场景一: 在Maven多模块化项目中,可以使用maven-dependency-plugin将被依赖模块jar文件中class文件提取出来放在指定位置。

<!-- 将依赖模块的jar包文件提取出来放到指定位置 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>com.xxx</groupId>
                        <artifactId>xxx-xxx</artifactId>
                        <version>1.0.0</version>
                        <type>jar</type>
                        <includes>**/*.class</includes>
                        <overWrite>false</overWrite>
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

场景二:将scope为system的依赖jar包一起打包

<!-- 打包scope为system的jar包 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.0.2</version>
        <executions>
            <execution>
                <phase>prepare-package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <includeScope>system</includeScope>
            <outputDirectory>${project.build.directory}/classes</outputDirectory>
        </configuration>
</plugin>

场景三:将scope为system的依赖jar包中的class文件解压出来重新打包

<!-- 将scope为system的依赖jar包中的class文件解压出来重新打包 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <includeScope>system</includeScope>
        <outputDirectory>${project.build.directory}/classes</outputDirectory>
    </configuration>
</plugin>

4. maven-resources-plugin

https://maven.apache.org/plugins/maven-resources-plugin/ maven默认使用该maven-resources-plugin资源文件,不需要明确配置。

<resources>
    <resource>
        <!-- 指定资源目录 -->
        <directory>src/main/resources</directory>
        <!-- 不打包指定类型的资源 -->
        <excludes>
            <exclude>**/*.svn</exclude>
        </excludes>
    </resource>
    <resource>
        <directory>src/main/resources/profiles/${profile.dir}</directory>
        <includes>
            <include>*.properties</include>
        </includes>
    </resource>
</resources>

5. maven-jar-plugin

https://maven.apache.org/plugins/maven-jar-plugin/usage.html 使用该插件可以在打包jar文件时做一些事情,比如:定义MANIFEST.MF文件,过滤文件等。

<!-- 生成jar包时打包资源文件配置 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
        <excludes>
            <exclude>**/profiles/**</exclude>
            <exclude>**/jdbc.properties</exclude>
            <exclude>**/*.proto</exclude>
        </excludes>
    </configuration>
</plugin>

6. maven-source-plugin

https://maven.apache.org/plugins/maven-source-plugin/usage.html

打包项目源码。

<!-- 打包源码 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>3.0.1</version>
    <executions>
        <execution>
            <id>attach-sources</id>
            <goals>
                <goal>jar-no-fork</goal>
            </goals>
        </execution>
    </executions>
</plugin>

7. maven-compiler-plugin

https://maven.apache.org/plugins/maven-compiler-plugin/usage.html 打包时设置编译参数。

<!-- 打包编译参数 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>

【参考】 http://blog.csdn.net/bluishglc/article/details/6085209 使用assembly打出的独立可执行jar包启动时报错spring错误的解决方法 http://www.trinea.cn/android/maven-plugin/ Maven常用插件配置和使用 http://www.jianshu.com/p/861c0d3697d0 maven-assembly-plugin打包spring项目运行报错解决

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-10-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一. 自带核心插件
  • 二. 第三方插件
    • 1.maven-assembly-plugin
      • (1)打包独立运行的jar文件
      • (2)打包压缩文件
    • 2. maven-shade-plugin
      • 打包可独立运行的jar文件
    • 3. maven-dependency-plugin
      • 4. maven-resources-plugin
        • 5. maven-jar-plugin
          • 6. maven-source-plugin
            • 7. maven-compiler-plugin
            相关产品与服务
            容器服务
            腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档