- 在pom.xml中添加maven-source-plugin插件,maven生成 jar的同时生成 sources包
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.0.0</version> <!-- 绑定source插件到Maven的生命周期,并在生命周期后执行绑定的source的goal --> <executions> <execution> <!-- 绑定source插件到Maven的生命周期 --> <phase>compile</phase> <!--在生命周期后执行绑定的source插件的goals --> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin>
- 另一种写法
<!-- Source attach plugin --> <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>
- 发布到Nexus
maven-source-plugin提供项目自动将源码打包并发布的功能
# 执行 mvn clean install deploy
settings.xml配置
<server> <id>dev</id> <username>admin</username> <password>admin123</password> </server>
- pom.xml配置
<distributionManagement> <!-- 用于上传的仓库 --> <repository> <!-- 这里的id需要和settings.xml中的server的id一致 --> <id>dev</id> <name>nexus-third</name> <!-- third是一个hosted类型的仓库,用于存放第三方或自己的jar包 --> <url>http://192.168.2.109:8888/repository/third/</url> </repository> </distributionManagement> <build> <plugins> <!-- 要将源码放上去,需要加入这个插件 --> <plugin> <artifactId>maven-source-plugin</artifactId> <version>2.4</version> <configuration> <attach>true</attach> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
- 源码下载
# 下载所有源码 mvn dependency:sources # 下载所有源码和文档 mvn dependency:sources -DdownloadSources=true -DdownloadJavadocs=true # 下载源码的doc文件
- 下载指定的jar对应的源码和文档
以 spring-data-redis-2.1.10.RELEASE.jar 为例
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.1.10.RELEASE</version> </dependency>
# 下载源码 mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=org.springframework:data:2.1.10.RELEASE:jar:sources # 下载javadoc mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=org.springframework.data:spring-data-redis:2.1.10.RELEASE:jar:javadoc
idea自带下载源码的功能,点开一个没有源码的类,右上角点download sources 就可以直接下载