Maven 使用

Maven 打包 jar

spring-boot插件打包

1
2
3
4
5
6
7
8
9
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!-- 打包 -->
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<!-- 指定 src/main/resources下所有文件及文件夹为资源文件 -->
<resource>
<targetPath>${project.build.directory}/classes</targetPath>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*</include>
<!-- <include>**/*.xml</include> <include>**/*.properties</include> -->
</includes>
</resource>
<!-- 使用com.alibaba.dubbo.container.Main启动应用的话,要把spring配置放到/META-INF/spring下 -->
<resource>
<targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
<directory>src/main/resources/spring</directory>
<filtering>true</filtering>
<includes>
<include>spring-context.xml</include>
</includes>
</resource>
</resources>
<!-- more -->
<plugins>
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<mainClass>com.alibaba.dubbo.container.Main</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<!-- 打包jar时,把依赖的第三方jar包,放到lib/文件下 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<type>jar</type>
<includeTypes>jar</includeTypes>
<useUniqueVersions>false</useUniqueVersions>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

配置阿里云私有库

1
2
3
4
5
6
7
8
9
10
11
12
<mirror>
<id>aliyun-nexus</id>
<mirrorOf>central</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
<!--
<mirror>
<id>aliyun-nexus-public-snapshots</id>
<mirrorOf>public-snapshots</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/repositories/snapshots/</url>
</mirror>
-->

发布 jar 到私有仓库

Maven setting.xml 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<server>
<id>maven2-weixin-public</id>
<username>weixin</username>
<password>weixin</password>
</server>
<server>
<id>maven2-weixin-release</id>
<username>weixin</username>
<password>weixin</password>
</server>
<server>
<id>maven2-weixin-snapshots</id>
<username>weixin</username>
<password>weixin</password>
</server>

项目 pom.xml 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 发布 jar 到中央仓库 -->
<distributionManagement>
<repository>
<id>maven2-weixin-release</id>
<name>Nexus Release Repository</name>
<url>http://maven.ctim:8081/repository/maven2-weixin-release/</url>
</repository>
<snapshotRepository>
<id>maven2-weixin-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://maven.ctim:8081/repository/maven2-weixin-snapshots/</url>
</snapshotRepository>
</distributionManagement>

注意,pom.xml中的id要和setting.xml中配置的一致

发布源码

1
2
3
4
5
6
7
8
9
10
11
12
13
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

如果不发布源码,可以使用如下配置,只打包源码,不发布到私有仓库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>attach-sources</id>
<!-- 只打包源码,不发布元源码到私有仓库 -->
<phase>none</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

执行 deploy 发布 jar 到私有仓库

1
mvn clean deploy -Dmaven.test.skip=true

注意:release 版本只能上传一次,SNAPSHOT 版本可以无限次上传

使用私有仓库下载 jar

在 setting.xml 配置 mirror

1
2
3
4
5
6
7
8
9
10
<mirror>
<id>aliyun-nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
<mirror>
<id>maven2-weixin-public</id>
<mirrorOf>maven2-weixin-public</mirrorOf>
<url>http://maven.ctim:8081/repository/maven2-weixin-public/</url>
</mirror>

在 setting.xml 配置 profile 并激活

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<profile>
<id>wechat</id>
<repositories>
<repository>
<id>maven2-weixin-public</id>
<name>Nexus Release Repository</name>
<url>http://maven.ctim:8081/repository/maven2-weixin-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>

激活 profile

1
2
3
<activeProfiles>
<activeProfile>wechat</activeProfile>
</activeProfiles>
  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/e3a1de67.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!