views:

75

answers:

2

I'm converting a largish Ant build to Maven. As part of the Ant build, we have several steps which created Java classes by invoking one of the project's classes, simplified as:

javac SomeGenerator.java
java  SomeGenerator  generated # generate classes in generated/
javac generated/*.java

I've split each generator in its own Maven module, but I have the problem of not being able to run the generator since it's not yet compiled in the generate-sources phase.

I've tried something similar to

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1.1</version>
            <executions>
                <execution>
                    <id>generate-model</id>
                    <goals>
                        <goal>java</goal>
                    </goals>
                    <phase>generate-sources</phase>

                    <configuration>
                        <mainClass>DTOGenerator</mainClass>
                        <arguments>
                            <argument>${model.generated.dir}</argument>
                        </arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Which sadly does not work, for the reasons outlined above. Splitting the code generators into two projects each, one for compiling the generator and another for generating the DTOs seems overkill.

What alternatives are there?


Using Maven 2.2.1.

+3  A: 

You can execute the maven-compile-plugin in the generate-sources phase. Just add another execution before the existing execution and configure it so that it just picks up the sources for the generator.

Or split the project in two: build the generator with a separate POM and include the generator library as a dependency to the POM that's generating the sources.

Personally I would split the project. Keeps the build files cleaner and easier to maintain.

Andreas_D
Two projects it is then. Thanks.
Robert Munteanu
Go with Andreas' second suggestion. Split the generation code into another project, tie them together with a third, parent project.This might seem like overkill at first, but, trust me, the solution that KLE is *really* overkill. Creating a strange dependency (using sourceDirectory) between a parent and child project, and trying to shoehorn a POM project to create a JAR is the definition of overkill.
tobrien
+1  A: 

We faced the same problem. We wanted to respect Maven's behavior as closely as possible, to have no problems with plugins and so on... Fighting Maven is just too expensive!

We realized that the update frequency of the generated code was usually very different from the one of the code that we manually write, so separating the code had very good performance characteristics for the build. So we accepted to have our generated classes as a dependency of the manually written.

We adopted the following structure, that had just one little change from a regular maven config, a change in the source directory.

Parent project : Generations

We created a parent project for all our generations.

  • It has a JAR type if it contains code to be compiled, otherwise POM.
  • There we have our generating code, in /src.
  • It can compile in /target as usual.
  • It runs the generation, each generator producing code in a sub-directory of /target.

Note: if you want several generated results in the same jar, just put them in the same sub-directory.

Child jar projects : Generateds

  • It is a subdirectory of the Generations project.
  • It has a JAR type.
  • The source directory points to the sub-directory in the parent's target.

    <sourceDirectory>../target/generated1</sourceDirectory>

  • It compiles normally in its own /target directory.


That structure allows us to :

  • have as little modification to the standard maven layout as possible, so every maven command and plugin keeps working nicely.
  • scale nicely if you have several generators,
  • scale nicely if you want to generate several jars (we had a case wsdl2java where one generator produced code that should be split into several jars ; each child generated project would have the same source directory, but would be configured with an <includes> to handle only some of the classes).
KLE
This answer violates many core assumptions of Maven. Implementing the solution this way will give you a Maven project that is unsupportable. Adding a source directory that "tunnels" out into the target directory of a parent project is a recipe for disaster.For starters, your parent project has packaging "pom". There is no compilation in a "POM" project, so you have to manually configure the compile plugin goal in a parent project.You are creating a dependency between two projects (parent-child) that isn't defined using a real dependency. You are making an end-run around Maven.
tobrien
@tobrien You are right about compiling in the parent, I forgot this. That was the less intrusive solution we found, and no disaster happened, contrary to what happened for several other configurations we tried. We are open for better ideas though, as you must know maven better than we do.
KLE
@tobrien Actually, our parent project has no code to compile, it only triggers generations through wsdl2java and others... That's why it has POM type.
KLE