3

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>

1 Answers1

1

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.

user944849
  • 14,524
  • 2
  • 61
  • 83
  • I tried it, It is not working. It is not even copying code to target/classes whereas my current pom is copying generated code with .java extension. – Jaydeep Vishwakarma Nov 04 '17 at 02:02
  • Did you use the same configuration you had above, with only the phases changed? I did not retype all of the existing configuration. Assuming yes, next step is to run Maven with debug option (`-X`) set and check to see where the compiler plugin is looking for test sources. Then configure the plugins to ensure the destination for each is the source for the next one in the chain. – user944849 Nov 06 '17 at 04:08