Adding extra source path to your Maven build

Although it might be considered as bad practice I ran into a case in which I wanted to add an extra source path to my Maven build besides the default ‘/src/main/java’ path. In my situation I generate Java code with JAX-WS to create a web service based on an existing WSDL (I will show this in a separate post). Normally you generate this code in the ‘target’ folder so it is automatically build up in the compile step and cleaned up in the clean step. But like I said I wanted to be able to use the generated code in my Netbeans project as ‘source’ code but I did not want mix it with my own code.
So I added an extra source path as described in this post like this:

...
<!-- Plugin to add source path to build cycle -->
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
   <executions>
      <execution>
           <id>add-source</id>
           <phase>generate-sources</phase>
           <goals>
              <goal>add-source</goal>
           </goals>
           <configuration>
               <sources>
                    <source>${basedir}/generated/src/main/java</source>
               </sources>
           </configuration>
      </execution>
   </executions>
</plugin>
...

And to keep it clean I also added the generated source path to the ‘clean plugin’ like this:

...
<!-- Plugin to add delete path to build cycle -->
 <plugin>
   <artifactId>maven-clean-plugin</artifactId>
   <version>2.2</version>
   <configuration>
       <filesets>
           <fileset>
                <directory>generated/src/main/java</directory>
                <includes>
                    <include>**/*.java</include>
                 </includes>
           </fileset>
       </filesets>
    </configuration>
</plugin>
...

This configuration makes it possible to use the extra source path as a normal Maven path and it shows up nicely in my Netbeans project:

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in Maven and tagged . Bookmark the permalink.