Maven modules will not find dependencies with scalatest - scala

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.

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 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?

Packaging Maven multi-module project

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.

Maven Javadoc - Unable to generate Javadoc

I have the following dependency and build in my pom file. I'm able to manually create the javadoc with a Maven command. I can also succesfully perform a build. The output doesn't mention javadoc at all. I've also tried leaving out the output directory paths. POM File
Dependency section:
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
</dependency>
and then the build section:
<build>
<finalName>D</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
<configuration>
<outputDirectory>${project.build.directory}/javadoc</outputDirectory>
<reportOutputDirectory>${project.reporting.outputDirectory}/javadoc</reportOutputDirectory>
<version>2.8</version>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The Maven Javadoc plugin doesn't run by default and needs to be bound to one of the default Maven lifecycle phases.
Here's how I would write the plugin's configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
<configuration>
<outputDirectory>${project.build.directory}/javadoc</outputDirectory>
<reportOutputDirectory>${project.reporting.outputDirectory}/javadoc</reportOutputDirectory>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>site</phase>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
Notice how I added an extra phase element to the execution. This will bind it to the "site" goal so that javadocs are generated when you run mvn site. Check Introduction to the Build Lifecycle if you want one of the default Java build phases.
Also note that I ditched the version parameter; by default, it should use your POM's version anyway.

How to check and access javadoc/source for Maven Artifacts

I am writing a Maven plugin which needs to check if a certain project
dependency has javadocs and sources available... and if so, would like
to download them and archive them on a server.
I cannot find out how to check if the javadocs and source are available
or how to access them if they are.
Any help would be appreciated.
You can reference additional artifacts by adding the classifier tag to a dependency. The classifier is the additional part of the artifact's name in the repository, e.g junit-4.5-sources.jar
So to directly declare a dependency on the junit sources jar you can specify it as follows:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<classifier>sources</classifier>
<scope>test</scope>
</dependency>
If you want to download all the dependency sources, use the maven-dependency-plugin's copy-dependencies goal specifying the classifier sources. The following example defines two executions, one for sources and one for javadocs.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>sources</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<classifier>sources</classifier>
<failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
<outputDirectory>${project.build.directory}/sources</outputDirectory>
</configuration>
</execution>
<execution>
<id>javadocs</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<classifier>javadoc</classifier>
<failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
<outputDirectory>${project.build.directory}/javadocs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
If you want to package all the downloaded artifacts into a zip, you can use the maven-assembly-plugin to create an archive of the project. The example below are the contents of an assembly descriptor file to include the sources and javadocs directories:
<assembly>
<id>project</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<useDefaultExcludes>true</useDefaultExcludes>
<includes>
<include>${project.build.directory}/sources</include>
<include>${project.build.directory}/javadocs</include>
</includes>
</fileSet>
</fileSets>
</assembly>
To reference the assembly, add a plugin configuration to your pom. This assumes the above contents have been put in src/main/assembly/sources.xml (make sure it is defined after the dependency configuration above):
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/sources.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>