Changing order of applying maven spring-boot and war plugins - java

I am using spring-boot-maven-plugin to add spring boot to my application and also >maven-war-plugin to move the generated .war to target (local filesystem) destination.
Unfortunatelly, when I add both of them into pom.xml:
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<outputDirectory>C:/Location</outputDirectory>
</configuration>
</plugin>
</plugins>
What happens is that first the .war is generated. Than moved to C:/Location. And later the original .war in /target of my project build is getting updated with spring-boot-maven-plugin (it can be confirmed both by console output and greater file size of .war inside /target.
So I end up with correct .war in inccorect location and incorrect .war (before adding spring boot) in correct location.
Is there a way to make those steps happen in reverse order? To first apply spring boot and later move the result to C:/Location?
I've tried by adding:
<executions>
<execution>
<phase>package</phase>
</execution>
</executions>
to <plugin> for spring boot and:
<executions>
<execution>
<phase>install</phase>
</execution>
</executions>
for war plugin, but it seems to take no effect whatsoever (spring boot is applied after war plugin):
[INFO] --- maven-war-plugin:3.2.1:war (default-war) # app ---
[INFO] Packaging webapp
[INFO] Assembling webapp [app] in [G:\app\target\app-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [G:\app\src\main\webapp]
[INFO] Webapp assembled in [310 msecs]
[INFO] Building war: C:\Location\app-0.0.1-SNAPSHOT.war
[INFO]
[INFO] --- spring-boot-maven-plugin:2.2.4.RELEASE:repackage (repackage) # app ---
[INFO] Replacing main artifact with repackaged archive

Related

How to prevent Maven from compiling classes in src/test/java, which are generated during the generate-sources phase?

In our jOOQ integration tests, we're using the jOOQ code generation plugin to generate classes into the src/test/java directory. We're doing that because:
We want to check in generated sources, so we can detect regressions in the code generator more easily
The generated classes are used by the tests
So, the (simplified) plugin configuration looks like this:
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<executions>
<execution>
<id>some-id</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generator>
<target>
<directory>src/test/java</directory>
</target>
</generator>
</configuration>
</execution>
</executions>
</plugin>
Unfortunately, this seems to lead Maven to believe that the classes thus generated need to be compiled as well during the compile phase, as can be seen in the following log output:
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) # ... ---
[INFO] Compiling 25 source files to C:\...\target\classes
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) # ... ---
[INFO] Compiling 25 source files to C:\...\target\test-classes
... which makes no sense at all. Debug output hints at both src/main/java and src/test/java being included as compileSourceRoots:
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile' with basic configurator -->
[DEBUG] (f) compileSourceRoots = [C:\...\src\main\java, C:\...\src\test\java]
For the record, during the testCompile phase, as expected, only src/test/java is placed on the compileSourceRoots path:
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile' with basic configurator -->
[DEBUG] (f) compileSourceRoots = [C:\...\src\test\java]
For the record, we're using Maven 3.6.2
How can I prevent the src/test/java directory from being added to the compileSourceRoots variable?
A workaround (not very pretty) is to exclude the test classes from the compiler plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>com/example/test/**/*.java</exclude>
</excludes>
</configuration>
</plugin>
Another is to use the generate-test-sources phase, which we avoided so far, because of some unrelated side effects that we wanted to test already before the compile phase:
<phase>generate-test-sources</phase>
I'm definitely hoping for a better solution!

maven: copy resource from dependent jar to target folder

I need to copy a resource file (menu.xml) from the root of a dependent jar file to the root of the output directory of my current project, before the tests are executed during the build. The file must be available for the tests but also later for the main program using ...getClassLoader().getResourceAsStream("menu.xml").
I'm trying the solution suggested in this question but it's not working.
This is how the pom file looks like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>resource-dependencies</id>
<phase>process-test-resources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<type>jar</type>
<includeArtifactIds>pm1j-jar</includeArtifactIds>
<includes>menu.xml</includes>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
When I execute mvn clean process-test-resources I see the following output:
[INFO] --- maven-resources-plugin:2.7:testResources (default-testResources) # pm1-config ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-dependency-plugin:2.8:unpack-dependencies (resource-dependencies) # pm1-config ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.709 s
[INFO] Finished at: 2016-05-05T15:01:06-03:00
[INFO] Final Memory: 30M/458M
[INFO] ------------------------------------------------------------------------
The file menu.xml is not copied to the target folder. How can I see what could be possibly wrong? I tried to run maven on debug log level and could see that the configuration was parsed correctly, but the plugin doesn't log any additional information of what is happening.
...
[DEBUG] (f) includeArtifactIds = pm1j-jar
[DEBUG] (s) includes = menu.xml
...
[DEBUG] (f) outputAbsoluteArtifactFilename = false
[DEBUG] (s) outputDirectory = c:\Dev\workspaces\config\pm1j\pm1-config\target\classes
...
[DEBUG] (f) project = MavenProject: com.expersoft.pm1j:pm1-config:3-SNAPSHOT # c:\Dev\workspaces\config\pm1j\pm1-config\
pom.xml
[DEBUG] -- end configuration --
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.014 s
[INFO] Finished at: 2016-05-05T15:09:29-03:00
[INFO] Final Memory: 27M/328M
[INFO] ------------------------------------------------------------------------
After some help from the comments, I could figure out the issue. The jar holding the menu.xml file is not in the dependencies list of my project.
Here is a better explanation of what I was trying to achieve for better understanding:
My project is inside a maven multi module, and I need the information of this menu.xml which is maintained on another project of the same multi module. Apart from this file there's no other dependency to this other project, therefore, there's also no maven dependency to the jar.
In order to avoid duplication of the file in my project, I helped myself so far with the maven-resources-plugin, which copies the file from the source directory of the other project. I never really liked this solution though, because it's dependent on having the source of this other project on the file system.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/src/main/resources</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/../pm1-jJar/src/main/resources</directory>
<includes>
<include>menu.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Then I discovered the maven-depency-plugin and the unpack goal which can extract the file out of the downloaded jar from the repository. This looked like a much cleaner solution. I tried it out and it worked actually well. The problem with this solution though was that the unpack plugin acts only in a later maven phase after testing and I do have some tests using the file as well.
After some more research, I found the resource-dependencies plugin with the unpack-dependencies goal which looked like the perfect solution but at this stage I had completely forgotten that I actually don't have a dependency to the jar.
A nice way to copy files from your dependency is to use Goal unpack :
you can mention your files /directories in
dir/** --> include everything from a directory
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>resource-dependencies</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>group_id</groupId>
<artifactId>artifact_id</artifactId>
<version>1.0.0</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}</outputDirectory>
</artifactItem>
</artifactItems>
<includes>directory_to_include/**</includes>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
I attached goal unpack to the phase compile. Customise as per your need.
In order to use the maven-depency-plugin to unpack a jar before testing, the workaround is to use failsafe instead of surefire to have tests run after the unpacking.
Simon

Automatically executing dependency:build-classpath in maven

I am literally trying to do exactly this:
http://maven.apache.org/plugins/maven-dependency-plugin/usage.html#The_dependency:build-classpath_mojo
What's amazing is that after finding an explicit example of exactly what I want Maven to do.. I still can't get it to work.
From the command line, I can run ...
mvn -Dmdep.outputFile=classpath.txt dependency:build-classpath
... which does indeed produce a file called classpath.txt with the information I'd like.
I would like to be able to issue a command like "mvn compile" and have the production of this classpath.txt file be a part of that process. The example provided at the link above associates it with generate-sources, which to my understanding should suffice.
When executing a command like "mvn compile" with this pom snippet below, nothing regarding the build-classpath goal seems to execute.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>build-classpath</id>
<phase>generate-sources</phase>
<goals>
<goal>build-classpath</goal>
</goals>
<configuration>
<outputFile>myfile.txt</outputFile>
<mdep.outputFile>myFile1.txt</mdep.outputFile>
<ihavenoidea>whatgoeshere</ihavenoidea>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
And here is what I end up with:
$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building someproj 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # someproj ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # someproj ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.600s
[INFO] Finished at: Fri Jan 31 14:05:29 CST 2014
[INFO] Final Memory: 9M/156M
[INFO] ------------------------------------------------------------------------
$ ls
bin html log pom.xml resources sql src target test-output wwwroot
Your plugin definition is inside <pluginManagement>, which means that when you will declare a "real" execution of that plugin inside a pom that has this pom as parent (or this pom itself), it will use that configuration.
This is generaly a good idea to use <pluginManagement> when a common configuration has to be applied on multiple execution, through multiple modules in the same global project.
Here, I would personally keep the compiler plugin inside <pluginManagement>, as you probably always want that plugin to be configured like this, but I woul move the dependency-plugin inside the <plugins> section (outside the <pluginManagement> section, well yes, this can be confusive...)
You may think of <pluginManagement> as a kind of template. It's often used in parent POMs to define a common configuration. Only plugins in <build><plugins> are included in the build.
That said, Maven does do some "magic" depending on the packaging type. I answered a similar question here.

Adding Grails command to Maven phases -- what am I doing wrong?

I need to build my Grails project with Maven, and it is necessary to add an additional grails command. I'm using the grails-maven-plugin to create the pom file, and I can build the war file with $ mvn package
While building this application, I will need to execute another grails command, one the does not correspond directly to any of the maven build phases. Referring to the docs, I'm adding a second execution element to the grails-maven plugin, as follows:
<plugin>
<groupId>org.grails</groupId>
<artifactId>grails-maven-plugin</artifactId>
<version>${grails.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>init</goal>
<goal>maven-clean</goal>
<goal>validate</goal>
<goal>config-directories</goal>
<goal>maven-compile</goal>
<goal>maven-test</goal>
<goal>maven-war</goal>
<goal>maven-functional-test</goal>
</goals>
</execution>
<execution>
<id>stats</id>
<phase>init</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<command>stats</command>
</configuration>
</execution>
</executions>
</plugin>
For this example I'm trying to execute grails stats in the maven init phase. (Eventually, stats will be replaced by something more useful.) when I run:
$ mvn package
the ouput includes:
[INFO] [grails:validate {execution: default}]
[INFO] [grails:init {execution: default}]
[INFO] [grails:config-directories {execution: default}]
[INFO] [resources:resources {execution: default-resources}]
which evidently doesn't contain the execution of grails stats. I am able to execute the stats command through Maven directly, in the following way:
$ mvn grails:exec -Dcommand=stats
it only fails to execute when added as a goal in the pom.
I'm using Java 1.5.0_22, Grails 1.3.7, and Maven 2.2.1.
Based on the information I could find about this plugin, you should add the grails: prefix to your goals.
I have no experience with this plugin, so I could be wrong =)
References:
http://grails.1312388.n4.nabble.com/grails-maven-plugin-version-td2284532.html
http://www.grails.org/Maven+Integration
so for example: <goal>grails:init</goal>
OK, my problem was confusing goals and Maven phases. Notice I was trying
<phase>init</phase>
and I had tried other items from the list of goals, but these are not Maven phases.
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

How to use jboss with maven

I've built my project successfully. My war is located in target dir and I try to run the war on the jboss here is the part of pom.xml that says jboss where to look for war..
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<jbossHome>C:\jboss-4.2.2.GA</jbossHome>
<serverName>all</serverName>
<fileName>target/0.0.1-SNAPSHOT.war</fileName>
</configuration>
</plugin>
</plugins>
</build>
Now I start it with maven here is the message :
[INFO] [jboss:start]
[INFO] Starting JBoss...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
But the localost isn't working, what did I forget to do ?
I don't see the hard-deploy and start goals in your pom (the latter is apparently there somewhere if you get this output, just missing from your post). Also, I use my own server configuration instead of all - but that should not make a big difference in theory. And the fileName property is not used by the plugin, you don't need it. JBoss automatically finds your war file if it is deployed properly (that's why you need the hard-deploy goal).
My configuration looks like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<version>1.4.sp1</version>
<configuration>
<jbossHome>...</jbossHome>
<serverName>MyServer</serverName>
</configuration>
<executions>
<execution>
<id>redeploy-and-restart-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>hard-deploy</goal>
<goal>start</goal>
</goals>
</execution>
</executions>
</plugin>
Note that the JBoss maven plugin does only work properly if you bind your server to localhost and to the JNDI port 1099. I guess if you are using the all server configuration, you haven't tampered with those settings, but it's good to know nevertheless.
If your server is still not running, you should check the log files in the all/log directory to see what's going on.

Categories

Resources