Jaydeep Vishwakarma
Jaydeep Vishwakarma

Reputation: 31

How to compile java files which are getting generated at run time

I am looking to compile classes which is generating at runtime by Code Generate.java. I am successfully able to run the Generate.java at run time by exec-maven-plugin. It is generating code in generated-source-java. But this code is not getting compiled. I also want to add them in a single jar for same i am using maven-assembly-plugin.
Here is my pom snapshot.

    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.5.0</version>
    <executions>
      <execution>
      <id>build-test-environment</id>
      <phase>generate-test-resources</phase>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
    </executions>
    <configuration>
      <mainClass>com.test.Generate</mainClass>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
      <execution>
        <phase>prepare-package</phase>
        <goals>
          <goal>add-source</goal>
        </goals>
        <configuration>
          <sources>
            <source>${project.build.directory}/generated-sources-java</source>
          </sources>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <finalName>test</finalName>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <appendAssemblyId>false</appendAssemblyId>
    </configuration>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Upvotes: 3

Views: 1081

Answers (1)

user944849
user944849

Reputation: 14981

You are close; the plugins need to be bound to different phases. Currently the configuration is adding the new source directory during the prepare-package phase, which happens after all built-in compilation is complete. If you configure it to run earlier in the lifecycle, I think everything will "just work."

The phases for manipulating test code are:

  • generate-test-sources
  • process-test-sources
  • generate-test-resources
  • process-test-resources
  • test-compile

For this case, I would change the config as shown:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.5.0</version>
  <executions>
    <execution>
      <id>build-test-environment</id>
      <phase>generate-test-sources</phase>  <!-- generating source code -->
      <!-- rest of config -->
    </execution>
  </executions>
      <!-- rest of config, consider moving into specific execution -->
</plugin>
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>add-test-sources</id>
      <phase>process-test-sources</phase>  <!-- do something with generated test sources -->
      <goals>
        <goal>add-test-source</goal>
      </goals>
      <!-- rest of config -->
  </execution>
</executions>

Note that I changed the build-helper-maven-plugin goal too (to add-test-source), since this is test code we're dealing with.

More details in the Maven Lifecycle overview docs.

Upvotes: 1

Related Questions