前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >maven项目配置框架

maven项目配置框架

作者头像
编程随笔
发布2019-09-11 16:06:24
4620
发布2019-09-11 16:06:24
举报
文章被收录于专栏:后端开发随笔后端开发随笔

任何一个maven项目都会继承一个默认的父pom配置:Super POM,详见:https://maven.apache.org/guides/introduction/introduction-to-the-pom.html 。 在pom.xml中可以直接使用一些变量值,如:

代码语言:javascript
复制
${project.groupId}               The group id of current project
${project.version}          The version of current project
${project.build.sourceDirectory} The source directory of current project
${project.basedir}               The directory that the current project resides in.
${project.baseUri}               The directory that the current project resides in, represented as an URI. Since Maven 2.1.0
${maven.build.timestamp}         The timestamp that denotes the start of the build. Since Maven 2.1.0-M1

详见:https://maven.apache.org/guides/introduction/introduction-to-the-pom.html之Available Variables

一个实际完整的maven项目pom.xml文件配置框架:

代码语言:javascript
复制
<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    
    <!-- 模块参数 -->
    <artifactId>xxx-xxx</artifactId>
    <version>x.x.x</version>
    <packaging>war</packaging>
    <name>xxx</name>
    <url>xxx</url>
    
    <!-- 父模块,可选. -->
    <parent>
        <groupId>xxx</groupId>
        <artifactId>xxx</artifactId>
        <version>x.x.x</version>
    </parent>

    <!-- 定义属性 -->
    <properties>
        <!-- 项目编码 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 时间格式 -->
        <maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss'Z'</maven.build.timestamp.format>
    </properties>

    <!-- 依赖配置 -->
    <dependencies>
        <dependency>
            <groupId>xxx</groupId>
            <artifactId>xxx</artifactId>
            <version>x.x.x</version>
        </dependency>
    </dependencies>

    <!-- 定义profile: 将开发配置和生产配置分离开 -->
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profile.dir>dev</profile.dir>
            </properties>
            <activation>
                <activeByDefault>dev</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profile.dir>test</profile.dir>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profile.dir>prod</profile.dir>
            </properties>
        </profile>
    </profiles>

    <!-- 打包 -->
    <build>
        <finalName>xxx</finalName>
        
        <!-- 打包资源 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>.svn</exclude>
                    <exclude>.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/profiles/${profile.dir}</directory>
                <excludes>
                    <exclude>.svn</exclude>
                </excludes>
            </resource>
        </resources>
        
        <!-- 配置打包插件,如: 依赖处理,资源复制,压缩打包等 -->
        <plugins>
            <!-- 生成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>

            <!-- 打包源码 -->
            <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>

            <!-- 打包编译参数 -->
            <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>

            <!-- 将依赖模块的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>

            <!-- 打包可执行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>

            <!-- 构建时不执行单元测试 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-10-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档