I have a Java project which I am developing using Eclipse. Below is my POM.XML
file:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mbm</groupId>
<artifactId>properties</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Properties</name>
<description>Define and process program arguments</description>
<dependencies>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
</dependencies>
<pluginRepositories>
<pluginRepository>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>daily</updatePolicy>
</snapshots>
<id>apache-maven-repository</id>
<name>Apache Maven repository maven2</name>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- Configuration for version 2.2 -->
<arguments>
<argument>-Xlocator</argument>
</arguments>
<outputDirectory>${basedir}/target/generated-sources/jaxb</outputDirectory>
<packageName>com.mbm.properties.jaxb</packageName>
<sources>
<source>${basedir}/src/main/java/com/mbm/properties/schema/Properties.xsd</source>
</sources>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sablecc-maven-plugin</artifactId>
<version>2.4-SNAPSHOT</version>
<configuration>
<sourceDirectory>${basedir}/src/main/java/com/mbm/properties/grammar</sourceDirectory>
<outputDirectory>${basedir}/target/generated-sources/sablecc</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
This POM invokes SableCC (via the sablecc-maven-plugin), a compiler generator that produces Java source code to be compiled along with the rest of the project. This works and does generate viable Java source code.
The POM also invokes XJC (via the jaxb2-maven-plugin), another utility that produces Java source code to be compiled along with the rest of the project. This also works and generates viable Java source code.
Here is a snippet of the Maven output, produced during the compilation phase:
[INFO] Changes detected - recompiling the module!
[DEBUG] Classpath:
[DEBUG] C:\Users\mbmas_000\workspace\Properties\target\classes
[DEBUG] C:\Users\mbmas_000\.m2\repository\org\glassfish\jaxb\jaxb-core\2.2.11\jaxb-core-2.2.11.jar
[DEBUG] C:\Users\mbmas_000\.m2\repository\javax\xml\bind\jaxb-api\2.2.12-b140109.1041\jaxb-api-2.2.12-b140109.1041.jar
[DEBUG] C:\Users\mbmas_000\.m2\repository\org\glassfish\jaxb\txw2\2.2.11\txw2-2.2.11.jar
[DEBUG] C:\Users\mbmas_000\.m2\repository\com\sun\istack\istack-commons-runtime\2.21\istack-commons-runtime-2.21.jar
[DEBUG] Source roots:
[DEBUG] C:\Users\mbmas_000\workspace\Properties\src\main\java
[DEBUG] C:\Users\mbmas_000\workspace\Properties\target\generated-sources\sablecc
[DEBUG] C:\Users\mbmas_000\workspace\Properties\target\generated-sources\annotations
[DEBUG] Command line options:
[DEBUG] -d C:\Users\mbmas_000\workspace\Properties\target\classes -classpath C:\Users\mbmas_000\workspace\Properties\target\classes;C:\Users\mbmas_000\.m2\repository\org\glassfish\jaxb\jaxb-core\2.2.11\jaxb-core-2.2.11.jar;C:\Users\mbmas_000\.m2\repository\javax\xml\bind\jaxb-api\2.2.12-b140109.1041\jaxb-api-2.2.12-b140109.1041.jar;C:\Users\mbmas_000\.m2\repository\org\glassfish\jaxb\txw2\2.2.11\txw2-2.2.11.jar;C:\Users\mbmas_000\.m2\repository\com\sun\istack\istack-commons-runtime\2.21\istack-commons-runtime-2.21.jar; -sourcepath C:\Users\mbmas_000\workspace\Properties\src\main\java;C:\Users\mbmas_000\workspace\Properties\target\generated-sources\sablecc;C:\Users\mbmas_000\workspace\Properties\target\generated-sources\annotations; -s C:\Users\mbmas_000\workspace\Properties\target\generated-sources\annotations -g -nowarn -target 1.8 -source 1.8
Note the source roots. The first one:
C:\Users\mbmas_000\workspace\Properties\src\main\java
does not surprise me. It makes sense that Maven would expect Java source to be in that directory.
The second two lines do surprise me:
C:\Users\mbmas_000\workspace\Properties\target\generated-sources\sablecc
C:\Users\mbmas_000\workspace\Properties\target\generated-sources\annotations
I never specified these additional directories in the POM (nor do I even know how to do that, as inputDirectory
is a conspicuously missing parameter accepted by maven-compiler-plugin
). How did Maven know to included these? Also, why isn't
C:\Users\mbmas_000\workspace\Properties\target\generated-sources\jaxb
included as well, as this is jaxb2-maven-plugin
's designated output directory?
How do I modify this POM file such that
C:\Users\mbmas_000\workspace\Properties\target\generated-sources\jaxb
Is included in the Java compilation phase?
The particular plugin you are using - org.codehaus.mojo:sablecc-maven-plugin
- happens to add the <outputDirectory>
as the compile source root. That's why maven-compiler-plugin
is able to find it.
You may use org.codehaus.mojo:build-helper-maven-plugin
to add an additional source as described in the plugin documentation:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>some directory</source>
...
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>