Where is the source code?

1. Introduction

When we view the code of Maven package in idea, there will be a download source code in the upper right corner, so that we can obtain the source code corresponding to the jar package from the warehouse.

To obtain the source code, first upload the source file when uploading the build (project) to the warehouse.

The following is the description of the plug-in on Maven’s official website:

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.

The general meaning is to create a jar compressed file containing the source code of the current project. By default, the jar compressed file is created in the target directory

Tip: starting with version 3.0.0 of the plug-in, all properties that can be used through the command line are named based on the following schema Maven. Source. *

The following is the goal included in the plug-in:

  • 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.

In mavenforkWhat is it?

True means that it will fork a new one JVM To run the compiler. It’s a little slow, but isolation is better. In particular, you can specify a JVM that is different from what Maven started

2. How to use

2.1 creating Maven projects / modules

The first step, of course, is to build a maven project or module. There is no need to demonstrate too much here. Everyone will

2.2 add plug-ins to POM

      <!--  Bind the source plug-in to Maven's life cycle, and execute the goal -- > of the bound source after the life cycle
          <!--  Bind the source plug-in to Maven's life cycle -- >
          <!-- Execute the goals of the bound source plug-in after the life cycle -- >

The definition intercepted above is to configure Maven source plugin plug-in and bind goal-jar-no-forkTo the compile phase of the default life cycle, so that we can execute the goal of the plug-in by specifying the execution of paase

Now let’s try the plug-in. We can switch to the project in terminal and execute itmvn compileSee the effect:


If we don’t bind to a phase of the life cycle and want to execute the plug-in, what can we do? We can use it directlyGoal instead of phaseTo build.

For example, we can comment out all the contents under the exxcuations node above, and then execute MVN source: jar no fork on the command line to get the source packaged file

3. Usage suggestions

  1. In multi project construction, Maven source plugin will not work if it is placed in the top-level POM. It needs to be placed in a specific project
  2. After using the plug-in, the source file of the project will also be brought after deploying to the remote warehouse