As per Maven conventions, all project sources should be in the src
folder. However, there may be legacy projects that are organized differently and may have more than one source folder. Also, in some projects, we might generate sources dynamically from tools such as wsdl2java
. In such cases, Maven needs to be told about these additional source folders. Note that such projects may not work well in IDEs.
Use the following steps to include and exclude source files and folders in your Maven project:
project-with-additional-source
.pom
file:<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.9.1</version> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>src/main/source</source> </sources> </configuration> </execution> </executions> </plugin>
mvn compile
[INFO] --- build-helper-maven-plugin:1.9.1:add-source (add-source) @ project-with-additional-source --- [INFO] Source directory: C:projectsapache-maven-cookbookproject-with-additional-sourcesrcmainsource added.
target/classes
folder:We had an additional source folder called src/main/source
. We specified this in the configuration
section of the Build Helper Maven plugin. We also bound the add-source
goal of the plugin to the generate-sources
phase of the default
lifecycle.
As part of the default
lifecycle, the generate-sources
phase is run by Maven prior to the compile
goal. This invokes the add-source
goal of the plugin, which adds the additional folder and its contents for compilation.
In a similar way, additional test folders can be added to the build. The configuration would be identical to the earlier case, except for the execution
section, which would be as follows:
<execution> <id>add-test-source</id> <phase>generate-test-sources</phase> <goals> <goal>add-test-source</goal> </goals> <configuration> <sources> <source>src/main/source</source> </sources> </configuration> </execution>
We specify the add-test-source
goal instead of add-source
and bind it to the generate-test-sources
phase.
The Build Helper Maven plugin provides a number of other goals that meet specific project requirements. Here are some of them:
attach-artifact
: This is used to attach additional artifacts to be installed and/or deployed, besides the project artifact. This would be done by configuring the plugin as follows:<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.9.1</version> <executions> <execution> <id>attach-artifacts</id> <phase>package</phase> <goals> <goal>attach-artifact</goal> </goals> <configuration> <artifacts> <artifact> <file>some file</file> <type>extension of your file </type> <classifier>optional</classifier> </artifact> </artifacts> </configuration> </execution> </executions> </plugin>
maven-version
: This is used to set a property containing the current version of Maven, which can be used as required. To use the Maven version number in the manifest of the project JAR, we will configure the plugin as follows:<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.9.1</version> <executions> <execution> <id>maven-version</id> <goals> <goal>maven-version</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.2</version> <configuration> <archive> <manifestEntries> <Maven-Version>${maven.version}</Maven-Version> </manifestEntries> </archive> </configuration> </plugin> </plugins> </build>
3.144.119.170