how do this in maven pom.xml when i run mvn clean install in one single module ?
Compile main source code
Call main class inside source code that is compiled to generate another source code
Group no 1 and 2 and execute compile again
During package phase the original source code plus generated source code should be in the jar
Uhh. Your task seems fairly weird to me. Anyway, you could just write a script. I come from Gradle, but it's analogue to Maven, so: Create a task buildPipeline.
buildPipeline depends on buildProjectA, buildProjectB and a copy task, e.g. copySourceA.
buildProjectA builds source of ProjectA. Maven/Gradle merely looks at sources resources and builds the project in a regular fashion.
copySourceA ensures to copy whatever you need of codebase A to codebase B, which is elsewhere. copySourceA depends on buildProjectA.
finally buildSourceB depends on copySourceA (or mustRunAfter it), so that when buildSourceB is triggered, the assumption can be made, that A is built and its sources copied.
If you need to do something else, like executing a script in order to run / build something, you can swap the copy task for anything else, Gradle is able to call executables and I suppose Maven can too.
Finally I will say two things. So far I do not understand what you mean by
Call main class inside source code
If anything I imagine you call a JAR, where its main class is the entry point for a process to build the other project?...
Be that as it may - this all sounds like a pretty messed up project structure to me, so maybe you should refactor the code properly. Especially if this is something you have to maintain in the future.
One of solutions i tried and works .
Basically you force to call maven compiler plugin again after u generate additional java code generated
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>generate-code</id>
<phase>process-classes</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<classpathScope>compile</classpathScope>
<mainClass>MyClassGenerator</mainClass>
<arguments>
<argument>${generated.code.dir}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>addtoSourceFolder</id>
<goals>
<goal>add-source</goal>
</goals>
<phase>process-classes</phase>
<configuration>
<sources>
<source>${generated.code.dir}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<executions>
<execution>
<id>compile-generated-code</id>
<configuration>
<includes>**/*.java</includes>
</configuration>
<phase>process-classes</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Related
I am migrating my project from Maven to Gradle and i have a Java Class that i need to have compiled before the rest of the project. This class creates a file that will be used by the project.
Does anyone have an idea how to accomplish this task? Thanks
Edit:
The following is the Maven solution:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
<includes>
<include>PATH/TO/CLASS/AnnotationProcessor.java</include>
</includes>
</configuration>
</execution>
<execution>
<id>compile-project</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
The AnnotationProcessor scrapes and stores all annotation data that the project declares. This class is supposed to be compiled before the rest of the project. At least that is what the comment section says.
Seems like you have some class that needs to exist before the rest of the project can be built. One way to resolve this would be to extract the dependency to a separate module so you have a multiproject build... This post may help you achieve what you want.
Build order of Maven multimodule project?
You can also introduce custom tasks in Gradle that will enable you to have "dependsOn(someTask)"
See https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:task_dependencies for more/examples
I am trying to generate JAVA classes WSDLs and XSDs, but when I run mvn clean install, I see that the classes are generating from my first plugin in the logs, but my second plugin just deletes them. I have my build section written like this:
<build>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>generate-wsdl-to-java</id>
<phase>generate-sources</phase>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
.
.
.
.
.
</configuration>
<inherited>true</inherited>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.4</version>
<inherited>true</inherited>
<executions>
<execution>
<id>generate-xsd-to-java</id>
<phase>generate-sources</phase>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
.
.
.
.
</configuration>
<inherited>true</inherited>
</execution>
</executions>
</plugin>
When I reverse the plugins the classes generate fine without anything being over-written/deleted. I could keep it that way if i wanted to and move on, but I would like to know what am I doing wrong in this case. I am semi-new to Maven, so still understanding all the ins and outs. Do i have to wrap them around "pluginManagement" or something like that?
it really depends on, how you configured the output folders of this two plugins.
The defaults are ${project.build.directory}/generated-sources/wsimport and ${project.build.directory}/generated-sources/jaxb so I wouldn't expect them NOT to be overwritten.
anyway: if both plugins are meant to run within the same phase, then their order within POM defines their execution order - even pluginManagement would not change this. so there is nothing wrong about this
I have a Speedment project set up but I do not want to run the GUI Tool and I do not want to check in the generated files in my software repository (GIT). How can I make Speedment automatically regenerate all code on build?
In your pom.xml file, add an execution tag to the speedment-maven-plugin like this:
<plugins>
<plugin>
<groupId>com.speedment</groupId>
<artifactId>speedment-maven-plugin</artifactId>
<version>${speedment.version}</version>
<executions>
<execution>
<id>Generate code automatically</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
I'm generating pom files for old projects. Then I test em with mvn clean compile install. But sometimes the sourceDirectory points to the wrong folder and there is no error. How could I check if there is actually is somthing compiled, without changing the code only the pom or the mvn call.
Greetings
Jeff
If there are no sources compiled the file xxxx-sources.jar doesn't exist if you use the plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
I want to run the maven compiler plugin in a different phase and with different sourceDirectories and destinationDirectories such that code from directories other than src/main/java and src/test/java can be used.
I thought the solution would look something like the below, where the phase I was linking it to was pre-integration-test. However the properties for testSourceDirectory and testOutputDirectory don't seem to be specified in this way as they are in the section of the POM.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile mytests</id>
<goals>
<goal>testCompile</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<testSourceDirectory>${basedir}/src/inttest/java</testSourceDirectory>
<testOutputDirectory>${basedir}/target/inttest-classes</testOutputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Is there a way to get this plug-in to compile different directories in different phases without affecting its default operation?
The source directories are set outside the compiler-plugin inside the <build> element, so this won't work.
You can use the build-helper-maven-plugin's add-source and add-test-source to specify additional source directories for your integration tests, but this will not remove the existing source dirs.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>add-it-source</id>
<phase>pre-integration-test</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/inttest/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
If you bind the add-test-source goal to run just before the testCompile goal, your integration tests will be included. Note you want them to be output to target/test-classes so the surefire plugin will find them.
To handle removal of the standard test sources, I wrote a small plugin to modify the model to remove existing testSource locations before adding the ones for integration tests.
After more research it is apparent this is not actually possible in Maven 2 in the way I want, a hack of some form is necessary to introduce integration tests. While you can add additional directories (as suggested by Rich Seller) there is no plugin to remove the other sources or to compile a directory separately from the main compilation.
The best solution I have found for adding integration tests is to first use the build helper plugin to add the directory inttest directory to be compiled as tests.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/inttest/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Now in order to get the integration tests to execute on the integration-test phase you need to use excludes and includes to manipulate when they get run as below. This allow any custom parameters you might want (in my case an agent being added via argline).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/itest/**</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>inttests</id>
<goals>
<goal>test</goal>
</goals>
<phase>integration-test</phase>
<configuration>
<excludes><exclude>none</exclude></excludes>
<includes>
<include>**/itest/**/*Test.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>