1

I'm using maven-processor-plugin to generate my metamodel classes, now I'd like to ad the generated foldet to my build path to use the classes generated in my code. However when I run mvn install, my metamodel is generated correctly, but the folder is not added as source folder. Here's my 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>intl</groupId>
    <artifactId>intl</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>4.3.4.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.28</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>16.0.1</version>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <version>2.2.4</version>
                <executions>
                    <execution>
                        <id>process</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-source</phase>
                        <configuration>
                            <processors>
                                <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                            </processors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-source</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.build.directory}/generated-sources/annotations</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build> </project>

I have no error, maven says BUILD SUCCESS but no source folder added. Can someone see what I did wrong? Sorry if it's obvious I'm a newbie to all things maven-related.

Here's the output I got when running mvn install:

[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building intl 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ intl ---
[INFO] Deleting C:\Users\myuser\Workspace\intl\intl\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ intl ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\myuser\Workspace\intl\intl\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ intl ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 9 source files to C:\Users\myuser\Workspace\intl\intl\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ intl ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\myuser\Workspace\intl\intl\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ intl ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ intl ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:2.3:war (default-war) @ intl ---
[INFO] Packaging webapp
[INFO] Assembling webapp [intl] in [C:\Users\myuser\Workspace\intl\intl\target\intl-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Webapp assembled in [52 msecs]
[INFO] Building war: C:\Users\myuser\Workspace\intl\intl\target\intl-0.0.1-SNAPSHOT.war
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ intl ---
[INFO] Installing C:\Users\myuser\Workspace\intl\intl\target\intl-0.0.1-SNAPSHOT.war to C:\Users\myuser\.m2\repository\intl\intl\0.0.1-SNAPSHOT\intl-0.0.1-SNAPSHOT.war
[INFO] Installing C:\Users\myuser\Workspace\intl\intl\pom.xml to C:\Users\myuser\.m2\repository\intl\intl\0.0.1-SNAPSHOT\intl-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.771 s
[INFO] Finished at: 2014-03-30T13:39:30+01:00
[INFO] Final Memory: 16M/244M
[INFO] ------------------------------------------------------------------------
DeleteMePlease
  • 91
  • 1
  • 3
  • 8
  • "... but the folder is not added as source folder". What do you mean? You don't see it an your IDE? What would you expect to see? Please verify that the compiled classes are available under `target/classes`. If they are, all is fine. – Robert Scholte Mar 30 '14 at 10:04
  • Actually I'm not so sure anymore what build-helper-maven-plugin is meant to do. I thought that maven-processor-plugin was only generating .java files but that those would not be in a source folder, so not usable in my code and not available in my compiled application. So I thought that build-helper-maven-plugin's purpose was 1. to make the generated folder a source folder so I can use generated code, and 2. to include it in the build path and compile its code as .class too. – DeleteMePlease Mar 30 '14 at 11:22
  • However it appears that even when I delete build-helper-maven-plugin in my pom.xml I got metamodel .class generated alongside my regular compiled .class from my source folder. And also, even with build-helper-maven-plugin, my generated folder is never added as a source folder in my IDE, visually I don't see it a such, yeah, but most important I cannot use generated code. Here's what I got in Eclipse after my maven install, whether or not I put build-helper-maven-plugin in my pom.xml: http://img15.hostingpics.net/pics/901287intl.png – DeleteMePlease Mar 30 '14 at 11:27
  • By default Eclipse will add the standard source folders to the eclipse-project ( i.e. `src/main/java`, `src/main/resources`, `src/test/java`, `src/test/resources`). For some well-known plugins Eclipse can also add their source-path to the eclipse-project. Otherwise you have to do it yourself. For Maven it doesn't matter. I assume that the processor-plugin registers its sourcefolder, so Maven will pick it up as part of the build lifecycle. – Robert Scholte Mar 30 '14 at 15:56
  • Indeed by manually adding the generated folder as a source folder everything works, but... what's the purpose of build-helper-maven-plugin then? – DeleteMePlease Mar 31 '14 at 10:06
  • For instance: The build-helper-maven-plugin can be used for old/custom projects where developers thought it was useful to have multiple source folders. – Robert Scholte Mar 31 '14 at 17:03

2 Answers2

0

You use srcas path for the generated sources, but add ${project.build.directory}/generated-sources/annotations as source folder.

Change the outputDirectory property of maven-processor-plugin to ${project.build.directory}/generated-sources/annotations, and every should work as intended.

Update

In your current logfile, neither process nor build-helper plugin are executed. This is due to a type in your phase. The phase is named generate-sources, but you wrote generate-source.

If your eclipse is recent enough, it should either work out of the box, or the project should show an "unknown lifecycle..." error, whose first quick fix (discover...) will download an m2e-buildhelper plugin which will show the generated sources in your eclipse as well.

blackbuild
  • 5,026
  • 1
  • 23
  • 35
  • Oops sorry I put the wrog version of my pom.xml. Initially, I did not specify any outputDirectory, and the metamodel was generated under ${project.build.directory}/generated-sources/annotations that's why I specified this directory in add-source but it doesn't work. And even if if specify ${project.build.directory}/generated-sources/annotations as outputDirectory explicitly, it still doesn't work :/ – DeleteMePlease Mar 30 '14 at 11:12
  • Show us your log, please. Allows, for completeness sake, please include an id in your build-helper execution (not the problem here but might lead to problems later) – blackbuild Mar 30 '14 at 11:16
  • Hum hum, which logs? (And where to find them?) really sorry, real ewbie here :$ Thank you very much for your time btw. I also answered Robert. – DeleteMePlease Mar 30 '14 at 11:42
  • Isn't "add-source" the build-helper's execution id – DeleteMePlease Mar 30 '14 at 11:44
  • ups, sorry, somehow overlooked that... sunday fever *G* – blackbuild Mar 30 '14 at 11:53
  • Just the output of the build when run on the console – blackbuild Mar 30 '14 at 12:26
0

@DeleteMePlease: neither maven-processor-plugin nor build-helper-maven-plugin show up in the log. I.e. neither of them are executed. As stated by @blackbuild, it's due to a typo mistake. When a phase is unknown, Maven ignores the associated goal(s) during a build.

But there is may be an other issue: in the pom, first maven-processor-plugin then build-helper-maven-plugin are declared in the section build/plugins with the same phase, i.e.: generate-sources. In such a case, the declaration order is the runtime order: maven-processor-plugin will run before build-helper-maven-plugin, i.e. before this last plugin has specified any folder as a source one. As maven-compiler-plugin will run later it shouldn't be an issue. If it is, then two options:

  • keep the two plugins in the same section but swap their order;
  • move build-helper-maven-plugin in an earlier phase, e.g. initialize.
atao
  • 835
  • 6
  • 13