本文介紹了使用Maven作為構(gòu)建工具的常用配置替裆,包括指定jdk版本体谒,jar包和依賴分開打包,多環(huán)境打包,配置私服缸沃,發(fā)布到私服等配置恰起。
主要內(nèi)容:
- 1.指定jdk版本
- 2.打包時(shí)jar和依賴分開打包
- 3.源碼打包
- 4.多環(huán)境打包
- 5.配置私服地址
- 6.打包到私服
1.指定jdk版本
在項(xiàng)目的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.打包時(shí)jar和依賴分開打包
用過SpringBoot的都知道執(zhí)行運(yùn)行package就可以打一個(gè)可執(zhí)行的jar,但是如果我們僅僅只改了很少的代碼每次都打一個(gè)jar趾牧,而且這個(gè)jar里包含了很多依賴检盼,每次上傳到服務(wù)器都會(huì)很慢。能不能將依賴和jar分開了翘单,這樣每次只需要更新jar即可吨枉。
如果是SpringBoot應(yīng)用先屏蔽SpringBoot的打包插件
<!-- 不使用SpringBoot的打包 -->
<!--
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
-->
在項(xiàng)目的pom.xml 中加入
<!-- 不將依賴打進(jìn)jar包 -->
<!-- 打包jar文件時(shí),配置manifest文件哄芜,加入lib包的jar依賴 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<!-- 指定運(yùn)行的主程序 -->
<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>
運(yùn)行
mvn clean package -DskipTests
3.源碼打包
但我們寫的jar給別人用時(shí)貌亭,有時(shí)需要源碼給別人,打成jar更方便
在項(xiàng)目的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.多環(huán)境打包
1认臊、在項(xiàng)目resources里新建多環(huán)境文件夾
2圃庭、在pom.xml里配置多環(huán)境
<!-- 通過命令mvn clean package -DskipTests -Pproduct 來指定打包環(huán)境 -->
<profiles>
<!-- 開發(fā)環(huán)境 -->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<package.environment>dev</package.environment>
</properties>
</profile>
<!-- 生產(chǎn)環(huán)境 -->
<profile>
<id>prod</id>
<properties>
<package.environment>prod</package.environment>
</properties>
</profile>
</profiles>
默認(rèn)使用開發(fā)環(huán)境dev
3、配置根據(jù)指定環(huán)境打包配置文件
<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>
<!-- 資源根目錄排除各環(huán)境的配置美尸,防止在生成目錄中多余其它目錄 -->
<excludes>
<exclude>prod/*</exclude>
<exclude>dev/*</exclude>
</excludes>
<filtering>true</filtering>
</resource>
<!-- 不同環(huán)境的配置文件選擇 -->
<resource>
<directory>src/main/resources/${package.environment}</directory>
</resource>
</resources>
按照上面的打包方式打包即可
mvn clean package -DskipTests -Pprod
其中-Pdev -Pprod 可以指定環(huán)境冤议,-DskipTests跳過測(cè)試
如果想要修改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>
<!-- 發(fā)布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>
<!-- 開發(fā)環(huán)境 -->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<package.environment>dev</package.environment>
</properties>
</profile>
<!-- 測(cè)試環(huán)境 -->
<profile>
<id>test</id>
<properties>
<package.environment>test</package.environment>
</properties>
</profile>
<!—準(zhǔn)生產(chǎn)環(huán)境 -->
<profile>
<id>pre</id>
<properties>
<package.environment>pre</package.environment>
</properties>
</profile>
<!-- 生產(chǎn)環(huán)境 -->
<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>
<!-- 指定默認(rèn)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>
<!-- 打包到私服用到,配置到項(xiàng)目的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.打包到私服
在項(xiàng)目的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>
運(yùn)行命令
mvn clean deploy -DskipTests -Pprod
將指定的環(huán)境發(fā)布到私服