views:

47

answers:

2

I have a Java program that generates Java classes for my application. Basically it takes in a simple spec for a class and generates a specialized form of Java bean. I want to integrate this into my Maven pom.xml so that if the input file is changed, Maven automatically generates the new .java file before the compile phase of Maven.

I know how to do this trivially in make but I didn't find anything in the Maven doc with this functionality.

A: 

Maven have phase "generate-sources" for this

splix
+1  A: 

You didn't provide much details on your code generation process but you could maybe simply invoke the code generator with the exec-maven-plugin (see the Examples section). The convention is to generate sources in ${project.build.directory}/generated-sources/<tool>. Then add the generated sources with the build-helper-plugin and its add-sources mojo. Bind every thing on the generate-sources phase.

I'll just show the build-helper stuff below:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>add-mytool-sources</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>${project.build.directory}/generated-sources/mytool</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

You could also write a simple plugin to wrap your generator. In that case, have a look at the Guide to generating sources.

PS: I may have missed something, there is a kind of mismatch between my answer and the title of your question.

Pascal Thivent
My program takes a .bean file and generates a .java file: java BeanGenerator Foo.beanwill generate a Foo.java so I'm trying to find a mechanism inMaven/ant that allows me to specify this relationship soit knows that when the .bean is newer than the .java it needsto run this comm and line. My frustration is that this wastrivial in ye olde worlde make but it's not at all obvioushow to do this Maven/ant.
Guy Argo
@Guy Ok. Then the suggested method will work (except it would run systematically).
Pascal Thivent