Maven 2: Deploying Project Sources

When you deploy your project to remote repository you may want to include the project source codes along with the project JAR file so that other projects would be able to access your source codes. It comes very handy when you use IDE like Eclipse or IDEA. Unfortunately by default Maven 2 does not deploy your source codes when you execute “deploy” goal.

This is how you can accomplish this. You need to configure source plug-in in your POM file

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-source-plugin</artifactId>
			<executions>
				<execution>
					<goals>
						<goal>jar</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

If you execute “deploy” goal now you will see that in your repository there will be another JAR file – yourproject-1.0-sources.jar.

You may also need to deploy your automated tests. This will help you to avoid writing duplicate code and eventually minimize the maintenance cost. If you want to deploy your test classes with your project you need to configure jar plug-in:

	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-source-plugin</artifactId>
			<executions>
				<execution>
					<goals>
						<goal>jar</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<executions>
				<execution>
					<goals>
						<goal>test-jar</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

And if you wish to include test sources then your POM file will look like this:

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-source-plugin</artifactId>
			<executions>
				<execution>
					<goals>
						<goal>jar</goal>
						<goal>test-jar</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<executions>
				<execution>
					<goals>
						<goal>test-jar</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

You can also put this into your super POM file and make all your project POM files extend this one. Then all your projects will deploy sources and tests by default.


Comments are closed.