Maven常用配置

本文介绍了使用Maven作为构建工具的常用配置,包括指定jdk版本,jar包和依赖分开打包,多环境打包,配置私服,发布到私服等配置。

主要内容:

  • 1.指定jdk版本
  • 2.打包时jar和依赖分开打包
  • 3.源码打包
  • 4.多环境打包
  • 5.配置私服地址
  • 6.打包到私服

1.指定jdk版本

在项目的pom.xml 中加入

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <compilerVersion>1.7</compilerVersion>
    </configuration>
</plugin>

2.打包时jar和依赖分开打包

用过SpringBoot的都知道执行运行package就可以打一个可执行的jar,但是如果我们仅仅只改了很少的代码每次都打一个jar,而且这个jar里包含了很多依赖,每次上传到服务器都会很慢。能不能将依赖和jar分开了,这样每次只需要更新jar即可。

如果是SpringBoot应用先屏蔽SpringBoot的打包插件

<!-- 不使用SpringBoot的打包 -->
<!--
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
-->

在项目的pom.xml 中加入

<!-- 不将依赖打进jar包 -->
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <classpathPrefix>lib/</classpathPrefix>
        <!-- 指定运行的主程序 -->
        <mainClass>me.jinkun.IblogApplication</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>
<!-- 拷贝依赖的jar包到lib目录 -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>copy</id>
      <phase>package</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.directory}/lib</outputDirectory>
        <overWriteReleases>false</overWriteReleases>
        <overWriteSnapshots>false</overWriteSnapshots>
        <overWriteIfNewer>true</overWriteIfNewer>
        <!--runtime scope gives runtime and compile dependencies,-->
        <!--compile scope gives compile, provided, and system dependencies,-->
        <!--test (default) scope gives all dependencies,-->
        <!--provided scope just gives provided dependencies,-->
        <!--system scope just gives system dependencies.-->
        <includeScope>runtime</includeScope>
      </configuration>
    </execution>
  </executions>
</plugin>

运行

mvn clean package -DskipTests
lib & jar 这里jar非常小

3.源码打包

但我们写的jar给别人用时,有时需要源码给别人,打成jar更方便
在项目的pom.xml里加入

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

4.多环境打包

1、在项目resources里新建多环境文件夹


新建dev & prod

2、在pom.xml里配置多环境

<!-- 通过命令mvn clean package -DskipTests -Pproduct 来指定打包环境 -->
<profiles>
  <!-- 开发环境 -->
  <profile>
    <id>dev</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <package.environment>dev</package.environment>
    </properties>
  </profile>
  <!-- 生产环境 -->
  <profile>
    <id>prod</id>
    <properties>
      <package.environment>prod</package.environment>
    </properties>
  </profile>
</profiles>

默认使用开发环境dev
3、配置根据指定环境打包配置文件

<resources>
  <resource>
    <directory>src/main/java</directory>
    <includes>
      <!-- 不带源码 -->
      <include>**/**.class</include>
      <include>**/**.xml</include>
    </includes>
    <filtering>false</filtering>
  </resource>
  <resource>
    <directory>src/main/resources</directory>
    <!-- 资源根目录排除各环境的配置,防止在生成目录中多余其它目录 -->
    <excludes>
      <exclude>prod/*</exclude>
      <exclude>dev/*</exclude>
    </excludes>
    <filtering>true</filtering>
  </resource>
  <!-- 不同环境的配置文件选择 -->
  <resource>
    <directory>src/main/resources/${package.environment}</directory>
  </resource>
</resources>

按照上面的打包方式打包即可

mvn clean package -DskipTests -Pprod

其中-Pdev -Pprod 可以指定环境,-DskipTests跳过测试

jar包里的目录结构

如果想要修改jar的名字
可以配置finalName指定特殊的名字

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <classpathPrefix>lib/</classpathPrefix>
        <mainClass>me.jinkun.IblogApplication</mainClass>
      </manifest>
    </archive>
    <!-- 重命名 -->
    <finalName>${artifactId}-${version}-${package.environment}</finalName>
  </configuration>
</plugin>

5.配置私服地址

修改setting.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <localRepository>D:\MavenRepository</localRepository>
    <servers>
        <!-- 发布jar到私服用到的密码 -->
        <server>
            <id>nexus</id>
            <username>admin</username>
            <password>admin123</password>
        </server>

        <server>
            <id>maven-public</id>
            <username>admin</username>
            <password>admin123</password>
        </server>
    </servers>
    <mirrors>
        <!--公司私服-->
        <mirror>
            <id>maven-public</id>
            <mirrorOf>*</mirrorOf>
            <name>maven-public</name>
            <url>http://xxx.xxx.xxx.xxx:7050/repository/maven-public/</url>
        </mirror>
    </mirrors>
    <profiles>
        <!-- 开发环境 -->
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <package.environment>dev</package.environment>
            </properties>
        </profile>
        <!-- 测试环境 -->
        <profile>
            <id>test</id>
            <properties>
                <package.environment>test</package.environment>
            </properties>
        </profile>
        <!—准生产环境 -->
        <profile>
            <id>pre</id>
            <properties>
                <package.environment>pre</package.environment>
            </properties>
        </profile>
        <!-- 生产环境 -->
        <profile>
            <id>product</id>
            <properties>
                <package.environment>product</package.environment>
            </properties>
        </profile>
        <!-- 依赖下载地址 -->
        <profile>
            <id>nexus</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <repositories>
                <repository>
                    <id>nexus</id>
                    <name>nexus</name>
                    <url>http://xxx.xxx.xxx.xxx:7050/repository/maven-public/</url>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <layout>default</layout>
                </repository>
            </repositories>
        </profile>
        <!-- 指定默认jdk版本可选 -->
        <profile>
            <id>jdk-1.8</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
            </activation>
            <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
        </profile>
    </profiles>
    <!-- 打包到私服用到,配置到项目的pom.xml里 -->
    <!--
    <distributionManagement>
        <repository>
            <id>maven-public</id>
            <url>http://xxx.xxx.xxx.xxx:7050/repository/maven-public/</url>
        </repository>
        <snapshotRepository>
            <id>nexus</id>
            <url>http://xxx.xxx.xxx.xxx:7050/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
    -->
</settings>

6.打包到私服

在项目的pom.xml加入

<distributionManagement>
  <repository>
    <id>maven-public</id>
    <url>http://xxx.xxx.xxx.xxx:7050/repository/maven-public/</url>
  </repository>
  <snapshotRepository>
    <id>nexus</id>
    <url>http://xxx.xxx.xxx.xxx:7050/repository/maven-snapshots/</url>
  </snapshotRepository>
</distributionManagement>

运行命令

mvn clean deploy -DskipTests -Pprod

将指定的环境发布到私服

参考

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270

推荐阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,363评论 6 343
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • 简介 概述 Maven 是一个项目管理和整合工具 Maven 为开发者提供了一套完整的构建生命周期框架 Maven...
    闽越布衣阅读 4,208评论 6 39
  • Maven的基本了解 什么是Maven? Maven就是Apache下的一个开源项目。它是用纯java开发的。是一...
    Bcome阅读 2,684评论 0 7
  • 【以前,以后】 夏夜,似乎更加的燥热,过了七点半的城市突然一下子沸腾起来。还没散去的热气交织着空气,霓虹,轰鸣,酒...
    袁仕琦私号老袁实在人阅读 216评论 0 1