本文介绍了使用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
将指定的环境发布到私服
网友评论