Multi-Modules maven pom - java

While writing for the first time a multi-modules maven pom, I wonder something.
First, here my parent pom :
...
<modelVersion>4.0.0</modelVersion>
<groupId>project.room_management</groupId>
<artifactId>room_management</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<name>room_management</name>
<modules>
<module>room_management_dao</module>
<module>room_management_domain</module>
<module>room_management_service</module>
<module>room_management_gui</module>
</modules>
...
and one of its children :
...
<parent>
<groupId>project.room_management</groupId>
<artifactId>room_management</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>project.room_management</groupId>
<artifactId>room_management_domain</artifactId>
<version>1.0</version>
...
Considering I don't need common modules to be share from parent to children poms, can I remove without "risks" the parent declaration into the children poms ? Or Maven does need it for modules compilation ?

You do not need the <parent> section in the modules.
This primarily mean that each module must be independent - you cannot share configuration sections through the parent.

In this example you need the parent element, cause you have dependencies between gui and your service (service to dao as well). Furthermore you need to define the dependencies between the modules to see which module uses which. On the other hand maven needs this information to predict the order of build of the modules which is important.
The next advantage of having such relationship is you can define the plugins/versions of dependencies in the parent module and many other things.

Related

how to handle maven internal dependancies?

Could someone let me know easy way to handle internal dependencies for maven projects. For now I have following things.
MainPorject depends on project A, B and C - Fat jar
Project A needs project B for compilation - Thin Jar
and project b depends on project c on compilation - Thin Jar
for now, I manually compile all the jar files from A,B and C project from their respective repos and put in mainProject to crate fat jar.
Is there a way I can provide config in such a way that when I compile mainProject it automatically fetches the latest code A,B and C repo? Same goes for project A and Project B.
Build a multi-module project that contains all three projects as modules. Then you always build everything with the latest code. And Maven takes care that everything is built in the right order.
You need a multi-module Maven project, with this setup:
<!-- parent -->
<groupId>com.stackoverflow</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<modules>
<module>C</module>
<module>B</module>
<module>A</module>
<module>Bundle</module>
</modules>
<!-- each module, optionally, if you want to let parent manage the dependency versions -->
<parent>
<groupId>com.stackoverflow</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
Parent pom.xml sits in a root directory and A, B, C, Bundle are direct children of the root directory.
<root>
| pom.xml
|
+---A
| pom.xml
|
+---B
| pom.xml
|
+---Bundle
| pom.xml
|
\---C
pom.xml

How to download source code from Maven Central for a project

I want to download the source code for the project XBee-api v9.3 from Maven Central so that I can modify the code for my application. I am using Netbeans and I have managed to produce a POM that validates and compiles successfully.
I have then run the command
mvn dependency:resources
from the same directory as the POM to force download of the source code.
I have a few problems:
When I look inside the project with Netbeans, I see the class files (which I think may actually be jars). When I open up any class, I only get the method headers, but not the source code.
When I open a class file, there is the option in the top right of the window to "Attach Sources...". When I select "Download", I get a message in the lower left of the window saying "Downloading source jar from known Maven Repositories for local repository file" but nothing seems to be happening.
When I open up the folder either with explorer or Netbeans files view, the folder is empty except for the POM. If I use Projects view in Netbeans, I can see a project structure and what appears to be the generated source files, but no source code.
I can't find answers on Google.
My questions are:
What am I doing wrong?
How do I download the source code?
Any help would be much appreciated.
<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.rapplogic</groupId>
<artifactId>xbee-api</artifactId>
<packaging>jar</packaging>
<version>0.9.3</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>A java library for communicating with XBee radios</description>
<url>https://github.com/andrewrapp/xbee-api/</url>
<licenses>
<license>
<name>GPL license, Version 3.0</name>
<url>https://www.gnu.org/licenses/gpl-3.0.html</url>
</license>
</licenses>
<developers>
<developer>
<name>Andrew Rapp</name>
<email>andrew.rapp+github.com#gmail.com</email>
</developer>
</developers>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<!-- https://mvnrepository.com/artifact/com.rapplogic/xbee-api -->
<dependencies>
<dependency>
<groupId>com.rapplogic</groupId>
<artifactId>xbee-api</artifactId>
<version>0.9.3</version>
</dependency>
</dependencies>
</project>
The GitHub project page is here:
https://github.com/andrewrapp/xbee-api
(referenced in the POM under the url element)
...so you can just download the source from the Github page: https://github.com/andrewrapp/xbee-api/archive/master.zip

Find in maven where a property is defined

I'm currently refactoring lots of pom.xml in various projects and git repo.
Sometimes, a pom in a project A will require an artifact defined in a project B in a version defined by a property :
<dependency>
<groupId>com.example</groupId>
<artifactId>artifact-from-b</artifactId>
<version>${version.from.somewhere}</version>
</dependency>
Sometimes, the version property is not obviously defined in the pom itself or its parent pom. It can be hidden in a parent's parent's parent...
I'm currently trying to find a way to resolve easily properties like ${version.from.somewhere} and find where it is defined.
Any idea of any tool that can help me (apart from eclipse, which fails for some tricky properties) ?
Thanks !
There is a related answer here Is there a way to trace origin of a property in maven pom?
That suggest using mvn help:effective-pom -Dverbose=true then you can find comments like com.example.model:2.1.0-SNAPSHOT.
I tried it and it worked for me.
In my case, the property is defined in the pom of com.example.model:2.1.0-SNAPSHOT in line 407
<dependency>
<groupId>com.example</groupId> <!-- com.example.model:2.1.0-SNAPSHOT, line 405 -->
<artifactId>model</artifactId> <!-- com.example.model:2.1.0-SNAPSHOT, line 406 -->
<version>1.0.6</version> <!-- com.example.model:2.1.0-SNAPSHOT, line 407 -->

Gradle mavenDeployer - how to include a separate modules code

I have a project like this:
MyProject
|-ModuleA
|-ModuleB
Module A is an Android Library that creates an aar, it has a dependency on Module B like so:
dependencies {
compile project(':ModuleB')
In ModuleA I am using mavenDepoyer to release locally:
uploadArchives {
repositories.mavenDeployer {
pom.groupId = "com.foo"
pom.artifactId = "bar"
pom.version = "1.0"
repository(url: "file://${localReleaseDest}")
}
}
This generates me an AAR file and a POM.
When uncompressed the AAR does not contain the class files from Module B
and the POM looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.foo</groupId>
<artifactId>bar</artifactId>
<version>1.0</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
<groupId>MyProject</groupId>
<artifactId>ModuleB</artifactId>
<version>unspecified</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
As you can see this declares that the AAR has a dependency on ModuleB with an unspecified version. And so if I use this this AAR/POM as a remote, it fails to resolve the dependency ModuleB.
Error:A problem occurred configuring project ':example'.
Could not resolve all dependencies for configuration ':example:_debugCompile'.
Could not find MyProject:ModuleB:unspecified.
Searched in the following locations:
https://jcenter.bintray.com/MyProject/ModuleB/unspecified/ModuleB-unspecified.pom
https://jcenter.bintray.com/MyProject/ModuleB/unspecified/ModuleB-unspecified.jar
Required by:
Test:example:unspecified > com.foo:MyProject:1.0
I do not want it to try and resolve Module B as another dependency, I want to use the mavenDeployer to be able to create the AAR & POM with Module B included inside, since I have the source code here to do that!
Searched the web to no avail, these sites gave hints but no answer:
How to publish apks to the Maven Central with gradle?
how to tell gradle to build and upload archives of dependent projects to local maven
http://www.gradle.org/docs/current/userguide/artifact_management.html
http://gradle.org/docs/current/userguide/userguide_single.html#sub:multiple_artifacts_per_project
http://gradle.org/docs/current/userguide/userguide_single.html#deployerConfig
As far as I know, AARs don't include their dependencies (only APKs do). Instead, transitive dependency resolution will take care of resolving not only the AAR but also its dependencies. The unspecified version is most likely a result of not setting the project.version property in ModuleB.
<dependency>
<groupId>MyProject</groupId>
<artifactId>ModuleB</artifactId>
<version>unspecified</version>
<scope>compile</scope>
</dependency>
The reason is your module dependencies is below :
compile project(':module B')
to resolve this issue, you should dependens maven dep
compile 'com.xxxxxx.xxx:xxxxx:1.0.0-SHNAPSHOT'

Apache Maven: What is the difference between Inheritance, Aggregation, and Dependencies?

I'm new to Maven, and I'm trying to understand why my company's modules are organized into 'module groups', but also each sub-module declares its parent explicitly. I don't quite understand what the POM Reference is trying to say about the difference between inheritance and aggregation.
For example, a parent module:
<groupId>example.group</groupId>
<artifactId>util</artifactId>
<packaging>pom</packaging>
<name>Util Parent</name>
<modules>
<module>util_client</module>
<module>util_core</module>
<module>util_server</module>
</modules>
And one of its children:
<parent>
<artifactId>util</artifactId>
<groupId>example.group</groupId>
<version>trunk-SNAPSHOT</version>
</parent>
<groupId>example.group.util</groupId>
<artifactId>util_core</artifactId>
<packaging>jar</packaging>
<name>Util Core</name>
Why declare it both ways? Is it redundant? To make things even more confusing, some of the util submodules depend upon eachother:
<groupId>example.group.util</groupId>
<artifactId>util_client</artifactId>
<packaging>jar</packaging>
<name>Util Client</name>
<dependencies>
<dependency>
<groupId>example.group.util</groupId>
<artifactId>util_core</artifactId>
</dependency>
</dependencies>
Sorry if this is a doozy of a question, but wow this is confusing! Thanks for your help.
When you define sub-modules, you can build and release them all at once from the top level.
When you use inheritance in the second example, you can use definitions from the parent POM defined once, (Like which versions of software to use)
In the last example, when one module needs resources from another module, you can add it as a dependency and it will download and include it in the build path automatically.

Categories