Packaging Maven multi-module project - deployment

I have a multi-module project that looks like this:
Module1
Sub-moduleEar
Sub-moduleJar
Sub-moduleEJB
Sub-moduleWar
Module2
Sub-moduleEar
Sub-moduleJar
Sub-moduleWar
Sub-moduleEJB
Is it possible to package Module1 and Module2 together in one file for deployment?

You could use maven dependency plugin
You have to add the lines of codes below to the pom of all submodules.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-artifact</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<type>${project.packaging}</type>
</artifactItem>
</artifactItems>
<outputDirectory>../Main/target/dependencies</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
See this.It would be helpful.

Related

Maven AspectJ plugin non spring project won't work

I have a project, which is NOT a spring application. I am trying to use AspectJ annotations in it. The Annotation classes are being referenced from another jar I have. I have mentioned my plugin section of POM below. My build succeeds but the console output of Maven never mentions anything about the AspectJ plugin and also the annotations don't work when I run my project.
I have been trying to find out what's wrong for hours now but can't figure it out.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<showWeaveInfo>true</showWeaveInfo>
<aspectLibraries>
<aspectLibrary>
<groupId>it.cvc.ciscocommerce.lps.lp-commons</groupId>
<artifactId>lp-commons</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>listpriceservice</warName>
</configuration>
</plugin>
<!-- Plugin for sdaas deployment. For compressing war to tar.gz -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/resources/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
These are the two dependencies defined in the Jar which I am trying to use as the aspect library.
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.7.4</version>
</dependency>
The jar is compiled fine and I amble to use it another SPRING application but not this one. In the SPRING application I don't even have the maven aspect plugin defined.
When I run the maven build, in console I see only the following plugins listed.
[DEBUG] Goal: org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean)
[DEBUG] Style: Regular
[DEBUG] Goal: org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile)
[DEBUG] Style: Regular
[DEBUG] Goal: org.apache.maven.plugins:maven-resources-plugin:2.6:testResources (default-testResources)
[DEBUG] Style: Regular
[DEBUG] Goal: org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile)
[DEBUG] Style: Regular
[DEBUG] Goal: org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test)
[DEBUG] Style: Regular
[DEBUG] Goal: org.apache.maven.plugins:maven-war-plugin:2.4:war (default-war)
[DEBUG] Style: Regular
EDIT: After reading kriegaex's answer and about pluginManagement vs plugins, I changed my POM as below. Please note that my project is not multi-module it has only one POM.
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<showWeaveInfo>true</showWeaveInfo>
<aspectLibraries>
<aspectLibrary>
<groupId>it.cvc.ciscocommerce.lps.lp-commons</groupId>
<artifactId>lp-commons</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>listpriceservice</warName>
</configuration>
</plugin>
<!-- Plugin for sdaas deployment. For compressing war to tar.gz -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/resources/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
When I do this, I get the following error on execution tag under AspectJ executions
Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:aspectj-maven-plugin:1.4:compile (execution: default, phase: process-sources)
UPDATE I am marking this question as answered as my original issue of AspectJ plugin not being invoked is solved. I will open a new question on my other issue. Thanks to kriegaex for pointing me to the right direction.
I think this one is a classic and no AspectJ problem at all but a beginners' error using Maven:
You have defined your plugins' default settings in the <pluginManagement> section but forgot to reference them later in a separate <plugins> section. Thus, Maven has no idea that you want to use them at all.
Update:
Okay, I will elaborate a bit more as you still seem to have problems understanding how to use <pluginManagement> vs.<plugins>: You use the former in order to define version, scope and default settings for your plugins. Then you use the latter in order to easily just reference the predefined (managed) plugin in whatever module of your (possibly multi-module) project you need them without copying / pasting the same version and configuration anymore. So it is not "use either this or that" but it is "use both and combine them in a smart way". Example:
<pluginManagement>
<plugins>
<plugin>
<groupId>my.group.id</groupId>
<artifactId>my-plugin-name</artifactId>
<version>1.2.3</version>
<configuration>
<something>foo</something>
</configuration>
</plugin>
<plugin>
<groupId>my.group.id</groupId>
<artifactId>my-other-plugin-name</artifactId>
<version>4.5</version>
<scope>test</scope>
<configuration>
<blah>xyz</blah>
</configuration>
</plugin>
</plugins>
</pluginManagement>
And then later in the same module or in another module having the former one as a parent or importing it as a BoM (Bill of Materials):
<plugins>
<plugin>
<groupId>my.group.id</groupId>
<artifactId>my-plugin-name</artifactId>
</plugin>
<plugin>
<groupId>my.group.id</groupId>
<artifactId>my-other-plugin-name</artifactId>
</plugin>
</plugins>
See? very clean and simple.
This is similar to the difference between <dependencyManagement> and <dependencies>, by the way.
You can also extend or override the configuration for a managed plugin in the <plugins> section, so you are not limited to what was preconfigured.
As for why some managed plugins worked without you explicitly mentioning them in the <plugins> section: They were configured either in your parent POM or in the Maven root POM, such as the dependency plugin, compile plugin and other predefined and preconfigured Maven base plugins. If you make Maven display the effective POM for your module, you will see them.
The AspectJ Maven plugin is of course not a Maven base plugin, thus you have to configure it by yourself, which is what you are trying to do.

How to generate xml report with scoverage maven plugin?

I'm trying to generate scoverage xml report to use it in Jenkins. But all I can get is the folder scoverage-classes with my .scala files. Here is a part of my pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<id>scala-compile</id>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>${scalatest.plugin.version}</version>
<configuration>
<junitxml>surefire-reports</junitxml>
<stdout>W</stdout>
</configuration>
<executions>
<execution>
<id>test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.scoverage</groupId>
<artifactId>scoverage-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
<aggregate>true</aggregate>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.9</version>
<reportSets>
<reportSet>
<reports>
<report>index</report>
</reports>
</reportSet>
</reportSets>
</plugin>
<plugin>
<groupId>org.scoverage</groupId>
<artifactId>scoverage-maven-plugin</artifactId>
<version>${scoverage.plugin.version}</version>
<reportSets>
<reportSet>
<reports>
<report>report
</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
Scalatest generates xml reports, but Scoverage does not. I've tried to run maven with different args like mvn scoverage:report, mvn scoverage:report site and so on. Nothing helps. What should I change in my pom to make it work?
It turned out that I have scala compiler plugin that is not supported by scoverage plugin. Also it looks like maven scala compiler plugin from org.scala-tools is outdated. Supported by scoverage plugins are:
net.alchim31.maven:scala-maven-plugin
com.google.code.sbt-compiler-maven-plugin:sbt-compiler-maven-plugin

How to pass custom configuration file to Akka application with Maven?

I'm developing an Akka application using Maven. For testing, I use scalatest. When I run the tests with the configuration in application.conf, everything goes smoothly. But when I try to use custom files it just doesn't work (e.g. I have one common.conf and other files that include that one for adjusting time-scale in Jenkins and so on). I tried running the tests with mvn -Dconfig.file=/path/to/myenv.conf test and -Dconfig.resource=/path/to/myenv.conf test but no luck. I'm using Akka 2.4.0 and Scala 2.11.7.
P.S. Here is the configuration of the plugins in my pom.xml:
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>scala-compile</id>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<!--arg>-make:transitive</arg-->
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
<!-- disable surefire -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- enable scalatest -->
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<stdout>W</stdout>
<!-- Skip coloring output -->
<junitxml>.</junitxml>
</configuration>
<executions>
<execution>
<id>scala-test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Thanks a lot!
Well, I actually found a way, just adding this to my scalatest maven plugin worked:
<argLine>-Dconfig.resource=/local.conf</argLine>
Is there a way to pass this externally to mvn test?

Porting a Maven Java project that uses JavaCC to Scala

I am trying to port a Maven based Java project that uses JavaCC to Scala.
I am happy to leave the JavaCC generated classes in Java at least for now.
However the JavaCC generated class depends on the classes being converted.
The project builds fine from the original Java sources, but if I replace them with the Scala sources Maven tries to compile the JavaCC generated class first and does not see and of the symbols provided by the new code.
The problem then seems to be how to get Maven to generate the Java code first then compile the Scala code and the generated code together that the compiler can find all dependencies. The solution seems to be to add some magical incantation to pom.xml. What should that be though?
My current pom.xml is:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>bobj</groupId>
<artifactId>bobj</artifactId>
<version>0.9</version>
<packaging>jar</packaging>
<name>BOBJ algebraic specification and verification system</name>
<url>http://github.com/fh-wedel/BOBJ</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<profiles>
<profile>
<id>scala-2.10</id>
<properties>
<scalaVersion>2.10.4</scalaVersion>
<scalaBinaryVersion>2.10</scalaBinaryVersion>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scalaVersion}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-swing</artifactId>
<version>${scalaVersion}</version>
</dependency>
</dependencies>
</profile>
</profiles>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>bobj.BOBJ</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>javacc-maven-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>javacc</id>
<goals>
<goal>javacc</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.6</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<args>
<!-- work-around for https://issues.scala-lang.org/browse/SI-8358 -->
<arg>-nobootcp</arg>
</args>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/scala</source>
<source>src/main/javacc</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
I did not write this. I started with the pom.xml that came with the project and and added the parts for scala and build-helper-maven-plugin
which I copied from other sources. My Maven-fu is minimal so I would not be surprised if I am doing something deeply misguided.
It turns out I forgot set up the scala-maven-plugin declaration for a mixed scala java project
ie the scala-maven-plugin needs
<executions>
<execution>
<id>scala-process-resources</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-process-test-resources</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
instead of the usual
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>

Maven modules will not find dependencies with scalatest

While working with scalatest I ran into a weird problem. I have a maven project with multiple modules. If I execute mvn test directly in the module. It works without problems, but if I do it in the root folder it complains about missing packages (dependencies) while compiling.
My configuration looks like following:
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_${scala.shortversion}</artifactId>
<version>3.0.0-SNAP2</version>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
</dependency>
plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<junitxml>.</junitxml>
<filereports>WDF TestSuite.txt</filereports>
</configuration>
<executions>
<execution>
<id>scala-test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
If I remove from maven-scala-plugin the goals it'll compile but scalatest will not find the test sources and exit with No tests were executed.:
<plugin>
<version>2.15.2</version>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
</plugin>
Any ideas on what I am doing wrong?!
Cheers
Hard to say without seeing all your poms, but generally speaking:
Define in root Pom all your dependencies in dependencyManagement section, your plugins in pluginManagement section with their configuration. In child poms define only those plugins and dependencies you need.
Keep in mind that xxxManagement sections only build shared definition, kind of root configuration. You still have to define in plugins and dependencies sections what you need but you can omit version and configuration elements.
This will allow all your child poms to run tests in all modules.