maven-source-plugin

源码在哪儿?

1. 介绍

咱们在IDEA中查看Maven包的代码时,右上角会有一个下载源码,这样咱们就能够从仓库中获取到jar包对应的源码。

要获取源码,首先要在上传构建(我的项目)到仓库的时候同时上传source(源码)文件。

上面是Maven官网对于该插件的形容:

The Source Plugin creates a jar archive of the source files of the current project. The jar file is, by default, created in the project's target directory.

大抵意思就是创立一个蕴含以后我的项目源码的jar压缩文件,默认状况下,这个jar压缩文件创立在target目录下

提醒:从插件的 3.0.0 版开始,所有能够通过命令行应用的属性都基于以下架构 maven.source.* 命名

上面是该插件所蕴含的goal:

  • source:aggregate aggregrates sources for all modules in an aggregator project.
  • source:jar is used to bundle the main sources of the project into a jar archive.
  • source:test-jar on the other hand, is used to bundle the test sources of the project into a jar archive.
  • source:jar-no-fork is similar to jar but does not fork the build lifecycle.
  • source:test-jar-no-fork is similar to test-jar but does not fork the build lifecycle.

maven中的fork是什么?

true 意味着它将创立(fork)一个新的 JVM 来运行编译器。这有点慢,但隔离更好。特地是能够指定一个不同于 Maven 启动的 JVM

2. 怎么应用

2.1 创立maven我的项目/模块

第一步当然是搭建一个maven的我的项目或者模块,这里就不必过多演示了,大家都会

2.2 pom中增加插件

<build>  <plugins>    <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>  </plugins></build>

下面截取的一段定义就是配置maven-source-plugin插件,并绑定goal- jar-no-fork到default生命周期的compile phase,这样咱们指定paase的执行就能够执行插件的goal

当初咱们来试一下该插件,咱们能够在terminal中切换到该我的项目下,而后执行mvn compile看成果:

如果咱们没有绑定到生命周期的某一个phase而想要执行这个插件怎么做呢,就能够间接应用goal而不是phase来构建。

例如,咱们把下面的exxcutions节点下所有的内容正文掉,而后在命令行执行mvn source:jar-no-fork也能够失去source打包后的文件

3. 应用倡议

  1. 如果在多我的项目的构建中,maven-source-plugin放在顶层的pom中是不会起作用的,须要放到具体的某一个我的项目中
  2. 应用了该插件,在deploy到近程仓库后也会带上该项目标source文件