How Maven implements creating a jar package with source code

Source: Internet
Author: User

Achieve goals

Maven packages, with the source code in the generated jar package. Remember, this source code means that the source code is placed in a jar file with the generated file instead of a separate Xxx-source.jar package.

Realize the idea

Treat the source code as a resource file

Implementation method One
<build>   <resources>      <resource> <directory>src/main/java</directory> </resource> </resources></build>

Description: The default resource folder is Src/main/resource, and this method modifies the resource folder so that your default resource folder becomes the Src/main/java folder. This folder is placed in Maven by default in the source code.
Cons: After modification, the Src/main/resource folder is not a resource folder, and the files placed in it will not be used as resources.

Implementation Method Two
<Plugin><Groupid>org.apache.maven.plugins</Groupid><Artifactid>maven-resources-plugin</Artifactid><version>2.3</Version><Executions><Execution><Id>copy-resources</Id><Phase>process-resources</Phase><Goals><Goal>copy-resources</Goal></Goals><Configuration><Outputdirectory>${project.build.outputdirectory}</Outputdirectory><Resources><Resource><directory>src/main/java</< Span class= "Hljs-title" >directory> <includes> <include>**/*.java</ include> </includes> </resource> </ resources> </configuration> Span class= "Hljs-tag" ></execution> </ executions></PLUGIN>   

Description: Plug-in Maven-resources-plugin default processing resources and test resources, add resources to the folder ${project.build.outputDirectory} , this reference represents the output folder of the project. is typically the project's WEB-INF\classes folder. When you are packing, package the contents of this folder into a jar file. So if you want to package the source code inside the jar file, you need to add the source code to the folder as a resource file ${project.build.outputDirectory} .
Cons: Code is relatively long
Advantages: Do not modify the resource folder, the project structure does not change, it is recommended to use this kind of

Reference Resources
      • http://stackoverflow.com/questions/25779900/how-to-include-source-file-java-file-parallel-to-class-file-in-the-generated
      • Http://stackoverflow.com/questions/23933911/include-source-code-while-exporting-a-jar-using-maven

How Maven implements creating a jar package with source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.