integration of sbt (play!) project with maven parent pom - maven

I have a maven project with multiple maven modules organized around parent pom. All these modules are packaged into JAR files which are dependencies of my Play! application built as a SBT project:
MyProject
-> pom.xml (parent)
MavenModule1
-> pom.xml (child pom)
MavenModule2
-> pom.xml (child pom)
PlayApplication
-> Build.scala (SBT project)
Since all maven modules are child projects of parent module (MyProject), I can go to 'MyProject', execute mvn clean install and entire project, except PlayApplication will be built.
The question is how can I modify parent pom/SBT build file to fire PlayApplication build together with the rest of modules?
(I know that probably there is no simple, build-in way to do it, so all hacks are welcomed :))

Use play2-maven-plugin - you can compile project as normal maven project.
Example of Build.scala (all changes should be synchronized between pom and Build.scala)
import sbt._
import Keys._
import play.Project._
import Path._
object ApplicationBuild extends Build {
val myExternalPom = super.settings ++ externalPom(baseDirectory(_ / "pom.xml"))
val appName = "PlayApplication"
val appVersion = "1.0.0"
val appDependencies = Seq(
jdbc,
anorm
)
val main = play.Project(appName, appVersion, appDependencies,file(".")).settings(
resolvers ++= Seq( "Local Maven Repository" at "file://"+Path.userHome.absolutePath+"/.m2/repository"
, "central" at "http://artifactory:8081/artifactory/libs-release")
).settings(
externalPom() :_*
)
}
Pom file
<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>test</groupId>
<artifactId>test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>play2</packaging>
<name>Play! Framework 2.x Maven Test Project</name>
<repositories>
<repository>
<id>typesafe</id>
<name>Typesafe - releases</name>
<url>http://repo.typesafe.com/typesafe/releases/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<play2.plugin.version>1.0.0-alpha1</play2.plugin.version>
<play2.version>2.1.0</play2.version>
<play2-scala.version>2.10</play2-scala.version>
<scala.version>2.10.0</scala.version>
</properties>
<parent>
<groupId>com.company.project</groupId>
<artifactId>my-parent-pom</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>${scala.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>play</groupId>
<artifactId>play_${play2-scala.version}</artifactId>
<version>${play2.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>${basedir}/app</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/conf</directory>
</resource>
<resource>
<directory>${basedir}</directory>
<includes>
<include>public/**</include>
</includes>
</resource>
</resources>
<directory>${basedir}/target/scala-${play2-scala.version}</directory>
<plugins>
<plugin>
<groupId>com.google.code.play2-maven-plugin</groupId>
<artifactId>play2-maven-plugin</artifactId>
<version>1.0.0-alpha1</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
Move all dependencies from Build.scala to pom file. Maven do not know about Build.scala (I do not test it)
NOTE
If you use the eclipse IDE (scala-ide) - please run command:
play eclipse always when you update pom file. This command updates .classpath . In idea I think dependencies are update automatically.
UPDATE
Add <directory>${basedir}/target/scala-${play2-scala.version}</directory> to pom file. Play by default compile all to this directory (problem in idea).
FOR IDEA COMMUNITY VERSION
You can use idea without play plugin - import your project as maven project to idea. Do not call play idea before.

Related

Inherit child-dependencies from maven dependency

I have two separate maven projects.
Project A contains utility classes and similar stuff. It also uses jetbrains annotations in some interfaces to mark parameters as Nullable and NotNullable.
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>20.1.0</version>
</dependency>
Project B uses some of the Utilities of Project A. It includes it as a dependency from my repository.
<dependency>
<groupId>org.test.group</groupId>
<artifactId>Utilities</artifactId>
<version>1.0</version>
</dependency>
I can access the classes from my utilities dependency just fine. But i do not see any of the annotations on the parameters. I also can't access the jetbrains annotations in project B in any of my classes there. I'd have to add the jetbrains dependency in project B as well to do so.
Is there any way to inherit the dependencies of another dependency?
I looked at other questions and found this similar one. Tho his solution was to set the optional-parameter to false which i am not even using. Perhaps also something that needs to be configured in the maven build? Currently im running my build with goals clean package deploy without any special additional configuration.
I know gradle builds allow for implementation and api dependencies, where one of them forwards the dependency to other projects that include it and the other doesn't.
Edit: Here the full configuration of my projects
Local nexus running under localhost:8081 containing my artifacts. Local TeamCity running under localhost:8080 used for the builds and deployment to the repository.
Project A pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>org.test.group</groupId>
<artifactId>Utilities</artifactId>
<version>1.0</version>
<name>Utilities</name>
<dependencies>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>20.1.0</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<distributionManagement>
<repository>
<id>local_nexus</id>
<name>Deployment</name>
<url>http://localhost:8081/repository/org.test.group/</url>
</repository>
</distributionManagement>
</project>
Project B pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>org.test.group</groupId>
<artifactId>TestProject</artifactId>
<version>1.0</version>
<name>TestProject</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.test.group</groupId>
<artifactId>Utilities</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>local_nexus</id>
<name>Deployment</name>
<url>http://localhost:8081/repository/org.test.group/</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>local_nexus</id>
<name>Deployment</name>
<url>http://localhost:8081/repository/org.test.group/</url>
</repository>
</distributionManagement>
</project>
Edit 2:
I've made some progress. Since i didn't change Project A's version after adding the annotations my local repository didn't fetch the new version. After a purge using mvn dependency:purge-local-repository the updated state of version 1.0 was available.
mvn dependency:tree prints now
[INFO] org.test.TestProject:jar:1.0
[INFO] \- org.test:Utilities:jar:1.0:compile
[INFO] \- org.jetbrains:annotations:jar:20.1.0:compile
Tho my IDE (Intellij) still doesn't recognize the annotation classes inside TestProject. Any idea why this is failing now?
You inherit annotations automatically, no configuration needed.
You don't inherit it, if it is of scope provided. Look at mvn dependency:tree to find out about the place and scope of the annotations library.
BTW: mvn clean package deploy is a waste of time, just use mvn clean deploy.
The issue was caused by my local maven repository. Since i didn't change the version of Project A's artifact (always 1.0) my local repository kept providing me with the old state of version 1.0.
Using mvn dependency:purge-local-repository i was able to cleanse the old 1.0 and load the new 1.0. Tho still my IDE refused to imports annotations dependency. At this point it was listed by mvn dependency:tree tho.
After tempering with the IDE for a while i decided to update my version to 1.1. Once Project A's 1.1 was built and Project B's dependency was updated to 1.1 it worked.
So basically it resulted from poor versioning of my projects, which interfered with my local maven repository.

Deploy Two Wars to a Repository with One Pom

I have a single project that builds two war files. The only difference between the two are configuration files and the war name. I'm trying to deploy the wars to our Nexus repository for distribution management. I know how to name the wars differently in the pom by using:
<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>
<artifactId>example</artifactId>
<version>1.1.2</version>
<packaging>war</packaging>
<properties>
<buildVersion>counter</buildVersion>
</properties>
<build>
<finalName>example_${buildVersion}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>nexus.example</id>
<url>https://example.com/nexus/content/repositories/wars</url>
</repository>
</distributionManagement></project>
But, when I run mvn deploy, the name of the war that is deployed doesn't have the buildVersion so both wars are named the same.
I've found the deploy:deploy-file directive, but it needs the version of the war that's being deployed defined as a -D spec. Since I'm doing CD, I don't have the version on the command line within Bamboo.
Has anyone come across this situation before and what was done to accomplish it?

Compiling classes with dependent jar files in maven

I want to compile some java files which have multiple dependent jars and make a jar file. I have kept all dependent jars under src/main/lib. after running mvn clean install, i get compilation failure of the classes. Dependent jars are not being copied to class path it seems. Anyone can tell whats going wrong her.
<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>test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<repositories>
<repository>
<id>external-jars</id>
<name>external-jars</name>
<url>file://${project.basedir}\src\main\lib\</url>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>jars</groupId>
<artifactId>jars</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib/*</systemPath>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>finaljar</finalName>
<sourceDirectory></sourceDirectory>
</build>
</project>
You construction
<dependency>
<groupId>jars</groupId>
<artifactId>jars</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib/*</systemPath>
</dependency>
does not work. In Maven, each artifact is addressed through groupId, artifactId and version. You cannot add a directory, but only separate jars.
While it is possible to add jars through system paths, it is much better to use a repository. Look at the possibilities in
How to add local jar files to a Maven project?

how to avoid inheritance of <site> and <url> from Maven parent POM?

This question has no Maven multi module context
In my projects, I often use a parent POM which defines some Maven plugins.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>commonParent</artifactId>
<version>1.2.2</version>
<packaging>pom</packaging>
<build>
<plugins>
</plugins>
</build>
<distributionManagement>
<repository> </repository>
<snapshotRepository> </snapshotRepository>
<site>
<id>releases</id>
<url>dav:http://intra.nexus.de/repository/company-docs/${artifactId}</url>
</site>
</distributionManagement>
</project>
I have a small documentation for this parent POM, which is deployed to Nexus 3 using the maven site plugin. Works fine.
I use this parent in other projects (packaging war and jar), which have a documentation, too. And they have their own <site> and <url> elements:
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.company</groupId>
<artifactId>commonParent</artifactId>
<version>1.2.2</version>
</parent>
<artifactId>myProject</artifactId>
<version>2.0.0</version>
<packaging>war</packaging>
<distributionManagement>
<repository> </repository>
<snapshotRepository> </snapshotRepository>
<site>
<id>releases</id>
<url>dav:http://intra.nexus.de/repository/company-docs/${artifactId}</url>
</site>
</distributionManagement>
</project>
When I deploy the site with mvn site:deploy, I get this output:
Pushing D:\dev\myProject\target\site
to http://intra.nexus.de/repository/company-docs/commonParent../myProject`
How can I avoid the inheritance of <site> from the parent to my project?
From the documentation of Maven Site Plugin:
If subprojects inherit the site URL from a parent POM, they will automatically append their to form their effective deployment location.
But, as soon as the parent project is not the direct ancestor, Maven will generated inadequate url and site/url values.
E.g. with a parent project which is not the root project.

Maven Multi-Module Project, Not Resolving Dependencies

I have a multi-module maven project that I can't get to compile. I have a Nexus repository sitting on my local network, and it is working (IntelliJ Idea is able to resolve my dependencies which reside only in that repository), and I am building through Jetbrains TeamCity. I am fairly certain that TeamCity is working since several other build configurations I have set up still work (using the same settings.xml). I am a bit of a loss for what could be causing the issue. Here are my pom files:
Parent pom:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.company.product.plugins</groupId>
<artifactId>plugin-parent</artifactId>
<version>1.2-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>product-wireless-plugin</module>
<module>product-paging-plugin</module>
</modules>
<distributionManagement>
<repository>
<id>releases</id>
<url>http://192.168.2.192:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://192.168.2.192:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<pluginRepositories>
<pluginRepository>
<id>autoincrement-versions-maven-plugin</id>
<name>autoincrement-versions-maven-plugin</name>
<url>http://autoincrement-versions-maven-plugin.googlecode.com/svn/repo</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>autoincrement-versions-maven-plugin</artifactId>
<version>2.0-SNAPSHOT</version>
<executions>
<execution>
<id>update-pom-versions</id>
<goals>
<goal>increment</goal>
<goal>commit</goal>
</goals>
<phase>compile</phase>
<configuration>
<autoIncrementVersion>true</autoIncrementVersion>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
</project>
product-wireless pom:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>plugin-parent</artifactId>
<groupId>com.company.product.plugins</groupId>
<version>1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.product.plugins</groupId>
<artifactId>product-wireless-plugin</artifactId>
<version>0.1.2</version>
<distributionManagement>
<repository>
<id>releases</id>
<url>http://192.168.2.192:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://192.168.2.192:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>com.company.product</groupId>
<artifactId>product-common</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies>
</project>
product-paging pom:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>plugin-parent</artifactId>
<groupId>com.company.product.plugins</groupId>
<version>1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.product.plugins</groupId>
<artifactId>product-paging-plugin</artifactId>
<version>0.1.2</version>
<distributionManagement>
<repository>
<id>releases</id>
<url>http://192.168.2.192:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://192.168.2.192:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>com.company.product</groupId>
<artifactId>product-common</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies>
</project>
And the error I am getting is:
com.company.product.plugins:product-wireless-plugin
[13:54:16][com.company.product.plugins:product-wireless-plugin] Importing data from 'C:/TeamCity/buildAgent/work/40ac813105cf8bd7/product-wireless-plugin/target/surefire-reports/TEST-*.xml' with 'surefire' processor
[13:54:16][com.company.product.plugins:product-wireless-plugin] Surefire report watcher
[13:54:16][com.company.product.plugins:product-wireless-plugin] Downloading: repolocation/nexus/content/groups/public/com/company/product/product-parent/0.9.0/product-parent-0.9.0.pom
[13:54:16][com.company.product.plugins:product-wireless-plugin] Failed to execute goal on project product-wireless-plugin: Could not resolve dependencies for project com.company.product.plugins:product-wireless-plugin:jar:0.1.2: Failed to collect dependencies for [com.company.product:product-common:jar:0.9.1 (compile)]
I am at quite a loss while trying to debug this... does anyone have any suggestions?
There are several approaches / tools for troubleshooting this sort of problem.
For this "could not resolve dependencies" error, there is almost always a more detailed error message and/or stacktrace earlier in the build log. Maven logs are actually extremely verbose, to the point of having to search for the "root" error message several screens up from the build failure.
Re-run the build with the -X flag. Here is documentation of Maven command line switches
Another option is to use mvn dependency:tree to inspect the full graph of transitive dependencies. mvn help:effective-pom is another useful tool that prints out the pom.xml after considering your settings.xml, any active profiles, etc. Likewise mvn help:active-profiles
There are many problems in your multi-module build. The most important one is that you define a dependency:
<dependencies>
<dependency>
<groupId>com.company.product</groupId>
<artifactId>product-common</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies>
which seemed either not be existing in a repository or you have not access to the repository which contains it or your download has failed based on whatever reason (can't guess!). Are you using a repository manager like Artifactory, Nexus, Archiva? If not i recommend to start using one.
Apart from that you are using different versions for parent and the module in wireless-module:
<parent>
<artifactId>plugin-parent</artifactId>
<groupId>com.company.product.plugins</groupId>
<version>1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.product.plugins</groupId>
<artifactId>product-wireless-plugin</artifactId>
<version>0.1.2</version>
A multi module build should define the version only via the parent and not within the artifact which means the above should look like this:
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>plugin-parent</artifactId>
<groupId>com.company.product.plugins</groupId>
<version>1.2-SNAPSHOT</version>
</parent>
<groupId>com.company.product.plugins</groupId>
<artifactId>product-wireless-plugin</artifactId>
The module should not define a version itself, cause it will inherit it from the parent. Furthermore you can see that you have a module which defines a release version (1.2) whereas the parent defines a SNAPSHOT version. An application/modules which are under development should define a version which is a SNAPSHOT version which means a thing like 1.2-SNAPSHOTetc.
The same applies accordingly for the definition of the distributionManagement. This should be defined only once in the parent of the project.
BTW. If you have several project the best is to define a company parent which contains some default definitions like distributionManagement, pluginManagement, dependencyManagement etc.
Ok, I have solved the problem thanks to input from #noahlz. After utilizing the -X flag to debug my build, I was finding that the parent pom of "product-common" (product-parent) could not be found. After browsing my Sonatype Nexus repository, I discovered that my build system was only publishing new versions of the parent pom when new modules were added to it. So, even though my parent pom was on version 0.9.0, the repository had the latest version as 0.6.1. I suppose the "product-common" library was compiling correctly because it had access to the parent pom (with the 0.9.0 version number) at compile time. Either way, changing the parent pom version in "product-common" to point to the most recent in the repository resolved my build issues with my plugins.

Resources