Build Multiple EARs choosing subset of jars - maven

Our current working Maven structure builds one EAR and is as below
workspace
>parent proj (containing root pom.xml)
>ear proj
>web proj
>jar1
>jar2
>jarN
We now need to customize this such that we build multiple EARs (for different requirements) using a combination of jars.
So the suggested workspace will now look like this:
workspace
>parent proj (containing root pom.xml)
>ear projects
>ear1
>ear2
>web projects
>web1
>web2
>component jars
>jar1
>jar2
>jarN
and in ear1 project pom.xml we want to pick and choose the web projects and jar projects which we need.
The question is:
a) is this above structure possible - since the folders named "ear projects", "web projects", "component jars" do not actually have a POM and build nothing
b) is is possible for an ear1 pom.xml to be built using artefacts outside of the same folder?
Earlier project ear1 was parallel with the jar1 and jar2 and all were child modules of the root pom.xml. But in the proposed structure, ear1 is built using the modules not in parallel, but ../web projects/web1 ../component jars/jar1 and so on.
c) is there a better way to achieve this?

The offered workspace looks good. For clarity, I'll put it here (note, ear, web and components are all empty directories).
parent (contains root pom.xml)
|--- ear
|--- ear1 (contains ear1 pom.xml)
|--- ear2 (contains ear2 pom.xml)
|--- web
|--- web1 (contains web1 pom.xml)
|--- web2 (contains web2 pom.xml)
|--- components
|--- jar1 (contains jar1 pom.xml)
|--- jar2 (contains jar2 pom.xml)
|--- jarN (contains jarN pom.xml)
Since project dependencies are taken from repository (local or remote), there is no problem that modules aren't going to be located on the same level. You should declare them as usual dependencies. For example, in ear1 you can specify:
<dependencies>
<dependency>
<groupId>_your_groupId_</groupID>
<artifactId>_jar1_artifactId_</artifactId>
<version>_jar1_version_</version>
<dependency>
<dependency>
<groupId>_your_groupId_</groupID>
<artifactId>_web1_artifactId_</artifactId>
<version>_web1_version_</version>
<dependency>
</dependencies>
I'm not sure if I totally understand your needs, but I'll try to describe what I would do.
You can use profiles to specify which projects to build. For example, assume ear1 project depends on jar1, jar2, jar4 and web2 projects. In this case, in your root pom.xml file you can specify a profile like this:
<profiles>
<profile>
<id>ear1</id>
<modules>
<module>components/jar1</module>
<module>components/jar2</module>
<module>components/jar4</module>
<module>web/web2</module>
<module>ear/ear1</module>
</modules>
</profile>
</profiles>
If you run mvn package -P ear1, then Maven will build only those modules that are specified in <modules> section of the profile.

Related

How to Run SonarQube Findbugs Analysis for a project with multiple source directories

I have one multimodule maven project where there are source directories apart from 'src' where java file resides.
This is the folder structure
folder1
-pom.xml
pom.xml Contains modules defined like this:
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
<module>module4</module>
<module>module5</module>
<module>module6</module>
<module>module7</module>
<module>module8</module>
<module>module9</module>
<module>module10</module>
</modules>
Different modules are organized like this:
module1
-src
-gen
module2
-src
module3
-gen
module4
module5
-src
-gen
So, as you see, there are modules/projects which have either src or gen or both or doesn't have any of it.
When I run findbugs analysis, it picked only java classes from 'src' and skipped 'gen' (Natural as Maven model forces the analyzer to pick from only src)
So, in the Jenkins job configuration, I defined sources explicitly like this:
-Dsonar.sources=src,gen
-Dsonar.exclusions=src/test/java/**
When I run with this configuration, analysis fails for modules which doesn't have both src and gen. (module2, module3, module4)
So, how do I run the analysis to pick either src or gen or skip that module if either of them is not found ?
Thanks,
Ron
When using the SonarQube scanner for Maven, you can't specific properties that only apply to some of the modules using the command line.
In the modules where you want to modify the sources, add in the pom.xml a property. For example, in module5/pom.xml add:
<properties>
<sonar.sources>src,gen</sonar.sources>
</properties>

Maven: Create several web archives from different modules

I want to migrate about 15 Ant projects to Maven.
Current build with Ant:
project-1
project-2 (needs project-3 as dependency)
project-3
project-webresources (contains web resources needed by ALL other projects)
project-WAR (empty target project)
(project-1, project-2, project-3 contain additional web resources)
Now, different WARs have to be built:
For Deployment 1 (contains project-1 and project-webresources):
Java sources from project-1 are compiled and copied to project-WAR/WEB-INF/classes
Web resources from project-webresources and project-1 are copied to project-WAR/
For Deployment 2 (contains project-1, project-2, project-3 and project-webresources):
Java sources from project-1, project-2, project-3 are compiled an copied to project-WAR/WEB-INF/classes
Web resources from project-webresources, project-1, project-2 and project-3 are copied to project-WAR/
My plan to realize it with Maven:
The projects become modules and for every deployment I create a project which holds the needed modules and one target WAR artifact. For the project-webresources I use maven overlays.
Build-deployment-1:
(mvn install here to build the deployment-1 Web archive)
<packaging>pom</packaging>
<modules>
<module>project-1</module>
<module>project-webresources</module>
<module>WAR-deployment-1</module>
</modules>
WAR-deployment-1:
<packaging>jar</packaging>
<dependency>
<groupId>com.groupId</groupId>
<artifactId>project-webresources</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.groupId</groupId>
<artifactId>artifact-1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
For the second WAR there will be projects like Build-deployment-2 and WAR-deployment-2 and so on which use other module combinations.
My problem:
What is the best way to copy all web resources from project-1 to WAR-deployment-1? If I use maven-resources-plugin in project-1 to copy resources to WAR-deployment-1 the target directory is hard coded to WAR-deployment-1. What if project-1 is part of build-deployment-2 and files have to be copied to WAR-deployment-2?! Can I use build properties for this? Or is the assembly plugin a solution?

Large quantity of profiles in pom.xml

I have the maven project with five separate modules each of them have pom.xml with duplicated profiles. Is there any options to move all profiles from pom to separate file?
You can put the profiles into the parent pom. They get inherited during the build:
parent pom (with all common profiles)
- module1
- module2
- module3
- module4
- module5
You need to reference the modules correctly
from parent pom using the <modules> tag
from each module reference the parent using the <parent> tag

Intellij navigate through multiple maven projects

So I have a file structure that looks like this:
.git
.project
.classpath
app1
pom.xml
.classpath
.project
src
app2
pom.xml
.classpath
.project
src
app3
pom.xml
.classpath
.project
src
TheAppIWorkOn
pom.xml
.classpath
.project
src
TheAppIWorkOn uses jars from app1, 2, and 3 which are maven dependencies so when I need to edit something in app1, 2, or 3 it's a painful process. If I use the "jump to declaration" functionality it just shows me the locked jar. Is it possible to set it up so that when I "Jump to declaration" it take me to the actual code that I can make edits on?
If you would have a maven reactor build it would have made all the module setup for you. Now you only have dependencies to jar files and IntelliJ cannot know that you have the modules to depend on. But you can help IntelliJ by pointing out the module dependencies and after that you will be able to navigate between all the classes in the project.
The best way would be to create a pom.xml in the root directory of the project. This pom would keep together the different modules and also define the build order. When you want to open the project for the first time you just point IntelliJ to this pom file and all the dependencies would be resolved and setup so the navigation between classes in the different modules is a breeze.
Sample pom file for the root directory:
<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.stackoverflow</groupId>
<artifactId>Q16589702</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>${project.artifactId}-${project.version}</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>app1</module>
<module>app2</module>
<module>app3</module>
<module>TheAppIWorkOn</module>
</modules>
</project>
If you cannot do this for some reason then you'll have to inform IntelliJ about the module dependencies. You do this by opening up the Project Structure.
Then select the module TheAppIWorkOn and press the plus sign in the bottom and choose Module Dependency....
And there you can select all modules that you want to have as dependency.
Press Ok and then Ok again.
Now you will be able to navigate between the different classes in the project.
If app1, app2 and app3 also have dependencies between each other then you will have to do the same for them.
But the simplest way is definitely to have a pom file in the root project directory with all inter module dependencies there.

Maven site without parent directory

I have the following maven-setup:
parent-project
|- libraries
|- utilities
|- core
|- component1
|- component2
in the parent i define all dependency- and pluginmanagement and the infos about the dev-team. utilities containing functional artifacts like the website skin and checkstyle-config, libraries are bundle-artifacts for simplifying the dependencies. The site is build on the core-module and its sub modules. The parent-part of the core pom.
<parent>
<artifactId>parent</artifactId>
<groupId>my.group</groupId>
<version>3.0.0</version>
</parent>
<artifactId>core</artifactId>
<version>3.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
When staging or deploying the site, the page is located in a "core"-subdir (e.g. /target/staging/core/index.html). Is there a way to locate the site of core in the root?
Atm i use a "dirty hack". On the webserver i created a syslink with ln -s ./ core so he don't knows its the same dir.
Solution was, that maven generates that subdirectory only, when the url for deploying is inherited from the parent. All I need to do was move the distributionManagement section for site deploy and the url element from parent to core.

Resources