Maven: project setup - java

I am starting a new project as well as starting with Maven.
I am not sure my project-structure with Maven is decent and so I would like to ask. I describe the project with a little example.
The projects name should be MyPrj.
It consists of 3 parts (server, client, lib).
Server and client are programs (main) and lib does include helper-classes both will use (e.g. for logging).
I designed 3 maven-projects - 1 per part.
MyPrj/
client/
pom.xml, src, target, ...
server/
pom.xml, src, target, ...
lib/
pom.xml, src, target
The parts java package-names are
MyPrj.client
MyPrj.server
MyPrj.lib
The maven addressing is
groupId: MyPrj, artifactId: client, version: 0.1
groupId: MyPrj, artifactId: server, version: 0.1
groupId: MyPrj, artifactId: lib, version: 0.1
In them POM of client and server is a dependency to lib.
<dependency>
<groupId>MyPrj</groupId>
<artifactId>lib</artifactId>
<version>0.1</version>
</dependency>
Is that setup ok or not typical for a maven project?

Your Maven project looks good but you missed one thing : the parent.
Your MyPrj project should have its own pom.xml.
MyPrj/
pom.xml
client/
pom.xml, src, target, ...
server/
pom.xml, src, target, ...
lib/
pom.xml, src, target
In the pom.xml of MyPrj, you can declare the submodules of your Maven project as following :
<project>
...
<modules>
<module>client</module>
<module>server</module>
<module>lib</module>
</modules>
</project>
The parent of multi-module Maven project has a lot of interesting features (dependencies management, repositories, SCM management, ...).
I suggest you to take a look to the following documentation :
Guide to Working with Multiple Modules

Related

customized job in maven

I generated jars from Talend,and I suppose to use them in a maven project.After some research I know that I have to install this jars in the local maven repository using:
mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar
and then add a dependency:
<dependency>
<groupId>....</groupId>
<artifactId>...</artifactId>
<version>...</version>
</dependency>
But I don't know what to put exactly in the groupId,artifactId and version tag. Help plz
Go to the maven repository https://mvnrepository.com and search for your dependency. Click on the version number and it will show you the complete dependency tag for your talend. e.g
<!-- https://mvnrepository.com/artifact/org.talend.esb.job/org.talend.esb.job.api -->
<dependency>
<groupId>org.talend.esb.job</groupId>
<artifactId>org.talend.esb.job.api</artifactId>
<version>6.3.1</version>
You should specify what is this "Talend"?
Here is simple introduction to maven pom structure:maven pom doc
groupId: This is generally unique amongst an organization or a project.
artifactId: The artifactId is generally the name that the project is known by.
version: is last piece of specification which package to use.
You can find specification to mvn dependencies on maven repository page. Here is an example for Talend ESB jar (newest version):
<!-- https://mvnrepository.com/artifact/org.talend.esb.job/org.talend.esb.job.api -->
<dependency>
<groupId>org.talend.esb.job</groupId>
<artifactId>org.talend.esb.job.api</artifactId>
<version>6.3.1</version>
</dependency>
BTW if you are just using it locally , then you can install jar with any group Id,artifact Id and version you feel like. Just make sure you use the same in your dependencies in project POM.
However this is not the recommended approach, but if you are not sure about the maven coordinates( group Id, artifact Id and version) you can use above given hack.

Maven project that depends on other project

After i've been through some stackoverflow questions and documents about maven pom.xml relations i am still confused.
I have a project with its own pom.xml
-src/MyProject
-src/MyProject/POM.XML
Now i need to include another 3rd Party project
-src/MyProject
-src/MyProject/POM.XML
-src/OtherProject
-src/OtherProject/POM.XML
I need now to tell Maven when its building the MyProject POM.XML the other POM.XML has to be processed and included.
Reason: I need not only the jar built from OtherProject as a dependency, but what is more important: i need all the dependencies (jars) from the OtherProject as well for MyProject (and i dont want to enter every jar as a dependency manually for MyProject which are already correctly defined in the OtherProjects POM.XML).
What are possible solutions to do this?
You can make a multi-module maven application that consist of several projects (called "modules"). The multi-module application nests all the projects and the so-called master pom.xml, in which the application modules are defined. Each module will have to hold its own pom.xml, also.
So, your directory structure would like:
/application
|---/project1
|---/project2
|---pom.xml
The master pom.xml will define the application modules:
<project ...>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>project1</module>
<module>project2</module>
</modules>
<dependencyManagement> ... </dependencyManagement>
...
</project>
Defined like this, project1 will build before project2.

Master project - Maven dependencies in Intellij

I'm currently working with Intellij as IDE, and I have 4 projects:
- An Admin project (contains java & ember for the front-end)
- An WS project (contains the WS)
- An API project (contains the objects, dao's etc)
- A master project which only contains a pom to reference the other 3.
Admin and WS depend on API.
I deploy everything in JBoss. Maven clean-install works, but I can't deploy to jboss: I get a ClassNotFoundError: WS can't find classes in API
My pom of the master file looks like this:
<dependencies>
<dependency>
<groupId>API</groupId>
<artifactId>API</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>C:/.../api/target/API-1.0.jar</systemPath>
</dependency>
<dependency>
<groupId>WS</groupId>
<artifactId>WS</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>C:/.../ws/www/target/WS-1.0.war</systemPath>
</dependency>
<dependency>
<groupId>Admin</groupId>
<artifactId>Admin</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>C:/.../admin/www/target/Admin-1.0.war</systemPath>
</dependency>
</dependencies>
I use Maven 3.3.3 and Jboss WildFly 8.2.1
In Project Settings I've simply put the 3 Maven libraries (Admin, WS, API) under
In the Deployement tab of the Jboss condfiguration, the 2 wars (Admin, WS) and the jar (API) are listed
The errors I get:
Failed to define class ...ws.endpoints.helpers.WS in Module "deployment.WS-1.0.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link .../ws/endpoints/helpers/WS (Module "deployment.WS-1.0.war:main" from Service Module Loader) Caused by: java.lang.NoClassDefFoundError: .../dao/helpers/Dao Caused by: java.lang.ClassNotFoundException: ...dao.helpers.Dao from [Module "deployment.WS-1.0.war:main" from Service Module Loader]
What am I doing wrong? Is there a better way to deploy all this?
I've also tried to use the local Maven repository to find the correct depedencies (removed the scope and system path), but I can't find them, although they are correct located under the C:\users.m2 directory
Thanks in advance!
Specifying scope as 'system' means that your artifact is always available and is not looked up in a repository, so if you are building those artifacts during your build life cycle - "system" scope is not the right way to go.
I would recommend:
1. Try packaging your artifacts and deploy to JBoss manually, just to make sure that it works
2. Then try it out with Maven plugin for JBoss: http://docs.jboss.org/jbossas/7/plugins/maven/latest/examples/deployment-example.html

How to include a maven module outside of the project context?

I'd like to have a module in some kind of global project directory, so that I can include that module in all other projects that use that common code.
But how can I then tell a maven parent pom to include and compile this global shared module?
The following does not work:
svn/MyGlobalProject/project-commons/pom.xml //should be shared among different projects
svn/MyProject/web-parent/trunk/pom.xml //the parent pom used to build the application
svn/MyProject/web-parent/trunk/project-domain/pom.xml //submodule 1
svn/MyProject/web-parent/trunk/project-web/pom.xml //submodule 2
parent pom.xml:
<project>
<groupId>de.project</groupId>
<artifactId>project-parent</artifactId>
<packaging>pom</packaging>
<modules>
<module>project-domain</module>
<module>project-web</module>
<module>../project-commons</module> <!-- Error -->
</modules>
</project>
mvn package results in:
Child module trunk\project-commons of trunk\pom.xml does not exist
If you run mvn install on that global project, it will be installed in your local repository. Your other projects can then reference it as a dependency:
<dependency>
<groupId>whatever</groupId>
<artifactId>project-commons</artifactId>
<version>1.0</version>
</dependency>
The downside of this simplistic approach is that your other projects won't compile until you've checked-out project-commons and run mvn install.
A more advanced approach is to deploy a network-accessible repository (such as Artifactory or Nexus) which you can deploy global artifacts to. Artifactory has a community edition which is free. You can then list this repository in your settings file and Maven will resolve artifacts that are uploaded to it.
Using relative path to include some submodules is not a good practice...you will have a lot of problems.
Can not you just put the common project as a dependency of the parent module...
In this way you will have that "common project" in all the submodule that declare the parent project as a parent...
Why do you want to compile your "common" project every time you compile the parent pom?
Edited:
YOUR SVN:
svn/MyGlobalProject/project-commons/pom.xml //should be shared among different projects
svn/MyProject/web-parent/trunk/pom.xml //the parent pom used to build the application
svn/MyProject/web-parent/trunk/project-domain/pom.xml //submodule 1
svn/MyProject/web-parent/trunk/project-web/pom.xml //submodule 2
PARENT POM.XML:
<project>
<groupId>de.project</groupId>
<artifactId>project-parent</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>de.project</groupId>
<artifactId>project-commons</artifactId>
<version>${common.project.version}</version>
<dependency>
<dependencies>
<modules>
<module>project-domain</module>
<module>project-web</module>
</modules>
Just like this...
in that way project-domain and project-web will inherit that dependency, and you will able to use it everywhere you want in submodules...
Than the use of dependency management could be a good improvements

Maven with Jenkins - Update parent pom version of dependency

I have a pom file that looks similar to this
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>myApp</groupId>
<artifactId>myAppId</artifactId>
<packaging>war</packaging>
<version>1.2-SNAPSHOT</version>
<name>Maven Test Webapp</name>
<url>http://maven.apache.org</url>
<dependency>
<groupId>com.manydesigns</groupId>
<artifactId>portofino-war</artifactId>
<version>3.1.10</version>
<type>war</type>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>TestName</finalName>
</build>
</project>
If i run 'mvn release:prepare' on the above pom file, the version of the artifact changes. i.e. it becomes
<version>1.2</version>
Now lets say i have gone and updated the portofino-war application which is a dependency of this pom file. portofino-war is now at version 3.1.11 but the parent pom file points to version 3.1.10 as shown above.
Is there any way i can update the parent pom (either via Maven or Jenkins) file if a new version of portofino-war is built?
Thanks
Edit
The application uses Maven overlays - http://maven.apache.org/plugins/maven-war-plugin/overlays.html to build a war file from other war files. This means that if any of the dependent modules are built, the parent module has to be built to produce the final war file.
The problem is that at the moment if i build any of the modules, i have to manually update the version in the parent pom file to build using the correct module version.
Edit 2
Thanks for your help Ralph. Basically this is what i would like to achieve:
What i am trying to do is create a maven project that will build a war file based on several modules. Assume the modules have the following structure:
Module 1
customerModule
|-webapp
|-jsp
|-customer
|-findCustomer.jsp
|-addNewCustomer.jsp
|-deleteCustomer.jsp
|-src
|-com
|-mycompany
|-customer
|-FindCustomerAction.java
|-AddCustomerAction.java
|-DeleteCustomer.java
Module2
productModule
|-webapp
|-jsp
|-product
|-productCustomer.jsp
|-addNewProduct.jsp
|-deleteProduct.jsp
|-src
|-com
|-mycompany
|-product
|-FindProductAction.java
|-AddProductAction.java
|-DeleteProduct.java
Module3
commonModule
|-webapp
|-css
|-style.css
|-jsp
|-templates
|-coreTemplate.jsp
|-src
com
|-mycomany
|-common
|-Logger.java
|-Access.java
|-META-INF
|-MANIFEST.MF
|-context.xml
|-WEB-INF
|-lib
|-oraclejdbc.lib
|-log4j.lib
|-common.lib
|-struts-config.xml
|-tiles-def.xml
|-web.xml
Each of the modules shown will be developed by a different team. Each team produces a war file and installs it in the local maven repository. As an example the repository could look like this
com
|-customerModule
|-customerModule.v2.1.war
|-customerModule.v3.0.war
|-productModule
|-productModule.v3.0.war
|-productModule.v3.1.war
|-commonModule
|-commonModule.v0.5.war
|-commonModule.v3.0.war
Now the build manager uses the above war files to build the final deployable war file. I was originally planning to use maven overlays to merge the three war files. I did test the merging of war files with overlays and found that the following configuration worked. i.e. using dependencies:
Note: These dependencies are in the commonModule pom file
<dependency>
<groupId>com</groupId>
<artifactId>customerModule</artifactId>
<version>3.0</version>
<type>war</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>productModule</artifactId>
<version>3.1</version>
<type>war</type>
<scope>compile</scope>
</dependency>
If i then build the commonModule module, i end up with one war file that contains all the contents of Module1, Module2 and Module3. Which ends up with something like this:
MyApp.war
|-webapp
|-css
|-style.css
|-jsp
|-customer
|-findCustomer.jsp
|-addNewCustomer.jsp
|-deleteCustomer.jsp
|-product
|-productCustomer.jsp
|-addNewProduct.jsp
|-deleteProduct.jsp
|-templates
|-coreTemplate.jsp
|-META-INF
|-MANIFEST.MF
|-context.xml
|-WEB-INF
|-lib
|-oraclejdbc.lib
|-log4j.lib
|-common.lib
|-customerModule.jar
|-productModule.jar
|-classes
|-com
|-mycomany
|-common
|-Logger.class
|-Access.class
|-struts-config.xml
|-tiles-def.xml
|-web.xml
The above works but requires a bit of manual intervention for a full release. Here is an example of what would happen if a module is modified
Team 1 updating module 1
- Team 1 makes changes to module 1 and check in changes into CVS
- Team 1 installs module 1 onto the maven repository by issuing mvn:prepare and mvn:perform on module 1. This adds a new version of customerModule.war on to the local repository
- Team 1 updates the dependency version for module 1 in the pom file used to merge the war files (i.e. commonModule)
- Team 1 builds the deployable war file by building the commonModule.
The above does not use a multi module project as you suggested so i am trying to understand how the versioning would work with multi module projects. For example, lets say the multi module project will look like this
MyApp
|- productModule
|-pom.xml
|- customerModule
|-pom.xml
|- commonModule
|-pom.xml
|-pom.xml
What is the difference in adding the version number in each child module compared to just havin the version number on the parent?
You say that the child modules will use the parent version. Lets say the parent version is currently version 3.4, how will it know that it is supposed to use version 3.1 of the productModule.war?
And finally, if team 1 has made a change to one of the child modules, would they still need to build it and deploy it on to the maven repository as an individual product first before building the multi-module project or can this be done as one step?
How would this work if i want to make a patch release that does not necessarily mean using the latest version of a specific module?
Thanks
For the scenario described in the "Edit" part of the question, I would recommend to put all war files in a single: multi module project.
You may want to use a dependency range instead of updating the pom: http://docs.codehaus.org/display/MAVEN/Dependency+Mediation+and+Conflict+Resolution#DependencyMediationandConflictResolution-DependencyVersionRanges

Categories

Resources