Gradle gather all jar dependencies in multi project and build a ear - gradle

I am pretty new to gradle and I am trying to achieve the following
I have setup a multi project build, with the following structure
root:
:commonjar (java-library)
:war1 (war)
:war2 (war)
:externaljars (local repository for unmanaged jars)
:libs (ear)
I would like to automatically gather all runtime dependencies for commonjar, war1 and war2 and generate a deployable ear (libs) where all jars are stored inside libs.ear/lib
I have kind found a way to gather all jar using taks definition like this:
task copyDeps(type: Copy) {
from(subprojects.configurations.runtime)
into project.file(libDirName)
}
But no matter where I store the jars, the get not picked up by the "ear" task.
Just to explain, I need to make a dummy ear, with only jars to do remote deployment and create shared library definitions on IBM Websphere application server.
Any good suggestion to have an ear gather and package all jars (transitive as well) used in the entire project?
Thanks

You can simply configure the ear task to include the dependencies you want to have in there with something like
ear {
duplicateStrategy = DuplicateStrategy.EXCLUDE
lib {
from rootProject.subprojects.configurations.runtime
}
}

Related

How to test multiple maven project?

I have created multiple maven modules (e.g.: entity, service, repository, facade) , those jar contain all my business logic and I will package them as jar. Then I create a projectA which will depend on all jar file and package as war file. Everything is okay, but question is : How to test my individual jar component as there is not persistence.mxl in jar. PThe persistence.xml is only located in projectA. Please help...

Add non-osgi jars to RCP4 project

I am building an RCP4 application.
I have two non-osgi jars called a.jar and b.jar. Both jars have tons of non-osgi dependencies. One of the dependencies of a.jar is b.jar. So the hierarchy looks like this:
My application
|--a.jar
|----aDependency1.jar
|----aDependency2.jar
|----aDependencyN.jar
|----b.jar
|------bDependency1.jar
|------bDependency2.jar
|------bDependencyN.jar
Some of the bDependencyN.jars are different versions of the aDependencyN.jars
(An example is commons-logging-1.0.4.jar vs commons-logging-1.1.2.jar)
I need to directly reference a.jar and b.jar from my RCP4 application. In other words, when I write code, I will import packages from a.jar and b.jar)
Which is the best approach:
Use bnd 2.4 via command-line to turn all non-osgi jars into osgi ones. I then add every jar to my project via target file
Create a new project "Plug-in from existing JAR archives", and select a.jar and all of its dependencies and export it as a "deployable plugin and fragment" called a.with.libs.jar. I do the same with b.jar and create b.with.libs.jar. I then add those 2 new jars to my project via target file
Create a new project "Plug-in from existing JAR archives", and select a.jar and all of its dependencies, and b.jar and all of its dependencies and export it as a "deployable plugin and fragment" called ab.with.libs.jar. I then add the new jar to my project via target file
Is there a better approach than the suggestions above?
One option is to use bnd-platform (I am also the author) to manage third party dependencies and create OSGi bundles from them. You can use it with both dependencies retrieved from Maven repositories and local Jars (see the README). When you configure a Maven dependency it will also include the transitive dependencies. Under the hood it uses bnd. If needed you can also customize how the Jars are wrapped. bnd-platform is a plugin for Gradle, you can easily start with this template - just add your dependencies and provide the configuration as described in the project README (bundle symbolic names, versions) and run gradlew bundles. The created bundles can then be added to the target platform. You can also use bnd-platform to build a p2 repository / update site.

Building/deploying a EJB .jar with its dependencies

I am new to Java EE. I use Maven, Eclipse and jBoss/WildFly.
I have a war project. When I build the project all its dependencies are packaged inside the war file in WEB-INF/lib.
Now I am trying to create a ejb project (I have <packaging>ejb</packaging> in the pom.xml). I want to deploy it as a separate project (not as a part of the war). When I build the ejb project Maven does not package any dependencies in the jar.
How can I package/deploy a ejb .jar with its dependencies?
UPDATE: I'd like to avoid packaging EJBs in an .ear if it is possible. (I do not want to create one more Maven project).
The best solution is package your project as ear. But if don't want use ear, maven assembly plugin can help you to package all needed jars in one file. This solution is only for "proof of concept" variant, and cannot be used in production mode, by licences limitation for example.

Building all dependencies in Maven

I have a Ear, war and 2 jar projects in my eclipse.
I want to automatically build 2 jars, war and ear project, when i run the pom inside ear project.
I remember doing this in maven in the past. But i forgot since i lost touch working with Maven for few years now.
Someone please remind me of that..
I used dependency compile, but it is not building jar, when i build the ear directly.
Should i first run pom in jar? does it not build that jar automatically when i build ear?
Create a multi module build that will build it all for you in the reactor. Read more about it e.g. http://www.sonatype.com/books/mvnref-book/reference/pom-relationships-sect-pom-best-practice.html#pom-relationships-sect-multi-vs-inherit
I guess you should define tha .jar and .war projects as dependencies of your .ear project. It is also advisable to have a parent pom, where all the projects are defined as modules, including the .ear project.
In this book you can find a well explained step-by-step setup of a maven multimodule project (with downloadable code).
There is also a great working example of an enterprise multimodule project in the JBoss quickstart examples.

Creating EAR with Maven

project structure:
domain project (containing Pojos/Entities)
service project (EJB project)
service client project (interfaces for service project)
web project
ear project
parent project (just containing parent pom)
How do I have to configure the different poms?
Do I have to define dependencies like service project needs domain and service client project?
The structure of an generated EAR with Eclipse (export as EAR):
target
META-INF
lib (containing ServiceClient.jar,Domain.jar)
Service.jar (EJB project)
Web.war
pom.xml
How can I get this structure with Maven?
The Maven EAR plugin allows you to define the projects required in the various roles of your EAR. Have a look at the documentation. Naturally your sub-projects will need to refer to each other for compiling, but you can set their scope as provided to ensure you only get one copy of the jar in the built ear.

Resources