Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Maven generated-sources not getting compiled? [duplicate]

I have a plugin which generates sources under the target/generated-sources/wrappers directory. It's wired into the generate-sources phase like this:

<plugin>     <groupId>mygroupid</groupId>     <artifactId>myartifactid</artifactId>     <executions>         <execution>         <phase>generate-sources</phase>         <goals>             <goal>xml2java</goal>         </goals>         </execution>     </executions> </plugin> 

The problem is, when I use mvn deploy the .class files won't be placed in the jar. I see all the .java files there, but no .class.

I read all the issues around this problem, but couldn't figure out how to solve the problem. I'm using Maven 3.0.x.

like image 510
acsadam0404 Avatar asked Oct 28 '13 11:10

acsadam0404


People also ask

What does Mvn generate sources do?

The “maven-source” plugin is used to pack your source code and deploy along with your project. This is extremely useful, for developers who use your deployed project and also want to attach your source code for debugging.

Is Maven compiler plugin necessary?

Maven Compiler Plugin might be the most important plugin in Maven. It is used to compile the sources of your project, which transform Java files ( *. java ) into class files ( *.

What is default compile in Maven?

The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax. tools. JavaCompiler (if you are using java 1.6) and is used to compile Java sources.

What is the Maven lifecycle?

Maven Lifecycle: Below is a representation of the default Maven lifecycle and its 8 steps: Validate, Compile, Test, Package, Integration test, Verify, Install and Deploy.


2 Answers

The build-helper plugin indeed solved the problem. Thanks @Joe for the comment.

<plugin>     <groupId>org.codehaus.mojo</groupId>     <artifactId>build-helper-maven-plugin</artifactId>     <executions>         <execution>             <phase>generate-sources</phase>             <goals>                 <goal>add-source</goal>             </goals>             <configuration>                 <sources>                     <source>${project.build.directory}/generated-sources/wrappers</source>                 </sources>             </configuration>         </execution>     </executions> </plugin> 
like image 189
acsadam0404 Avatar answered Sep 22 '22 09:09

acsadam0404


If you have written the plugin by yourself you can programatically add the path with the generated sources to the maven source paths.

@Mojo(name = "generate") public class MyCodegenMojo extends AbstractMojo {     @Parameter(defaultValue = "${project}")     private MavenProject project;      @Override     public void execute() throws MojoExecutionException, MojoFailureException{         // your generator code          project.addCompileSourceRoot("path/to/your/generated/sources");     }  } 

For example the raml-jaxrs-codegen plugin uses this technique. See RamlJaxrsCodegenMojo.java for more details.

like image 29
Markus Peröbner Avatar answered Sep 19 '22 09:09

Markus Peröbner