为基于Maven的项目生成源代码jar

时间:2020-02-23 14:37:20  来源:igfitidea点击:

在本教程中,我们将看到如何为基于Maven的项目生成源代码jar。

如果我们有简单的Java项目,那么我们需要转换IT Maven项目。

有时,我们需要使用项目jar包装源代码。
当人们使用库时,这非常有用,并且可以添加调试源代码。

你可以简单地使用 maven-source-plugin达到相同的。

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

在POM.xml中放置此项后,我们应该能够与项目jar查看源jar。

这是完整的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.igi.theitroad</groupId>
  <artifactId>theitroadMaven</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
   <build>
	  <plugins>
	    <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>
	 </plugins>
  </build>
</project>

部署jar

你需要运行步 mvn install生成源jar以及项目jar。
我正在使用Eclipse来运行此MVN命令。

我们也可以转到终端中的项目位置并执行 mvn install生成源代码jar。

这里是命令的输出。

[INFO] >>> maven-source-plugin:3.2.1:jar (attach-sources) > generate-sources @ theitroadMaven >>>
[INFO] 
[INFO] <<< maven-source-plugin:3.2.1:jar (attach-sources) < generate-sources @ theitroadMaven <<<
[INFO] 
[INFO] 
[INFO] --- maven-source-plugin:3.2.1:jar (attach-sources) @ theitroadMaven --
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ theitroadMaven --
[INFO] Installing /Users/apple/eclipse-workspace/theitroadMaven/target/theitroadMaven-0.0.1-SNAPSHOT.jar to /Users/apple/.m2/repository/org/igi/theitroad/theitroadMaven/0.0.1-SNAPSHOT/theitroadMaven-0.0.1-SNAPSHOT.jar
[INFO] Installing /Users/apple/eclipse-workspace/theitroadMaven/pom.xml to /Users/apple/.m2/repository/org/igi/theitroad/theitroadMaven/0.0.1-SNAPSHOT/theitroadMaven-0.0.1-SNAPSHOT.pom
[INFO] Installing /Users/apple/eclipse-workspace/theitroadMaven/target/theitroadMaven-0.0.1-SNAPSHOT-sources.jar to /Users/apple/.m2/repository/org/igi/theitroad/theitroadMaven/0.0.1-SNAPSHOT/theitroadMaven-0.0.1-SNAPSHOT-sources.jar
[INFO] -----------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] -----------------------------------------------------------------------
[INFO] Total time:  2.783 s
[INFO] Finished at: 2017-03-20T00:13:15+05:30
[INFO] -----------------------------------------------------------------------