Generating the .war file from maven dependency project - maven

Here I have two projects those are project(1) and project(2).
I am going to generate a .war file for the project(1) but it depends on project(2).
I built the project(2) as a .jar file and added it to the project(1)'s build path but while runnig mvn install it results in compilation errors like:
package com.disha.db.dao.orm.gen does not exist` the package is in the project(2).
Can any one please help to me.

You have to delegate dependency management to Maven and that's actually where it comes in hand, otherwise you can move on packaging and resolving inter-projects dependencies by hand and let the Maven alternative be dropped.
You should make sure you have provided the correct Project Object Module description for your projects (pom.xm) along with tha packaging type.
Then since you want the project(2) to be availble for project(1) at compilation time, you have to declare, project(2) as a dependency of project(1).
project(2) pom.xml (I will refer to it as project-2 since 'project(2)' does not match a valid id pattern.):
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>project2.group.id</groupId>
<artifactId>project-2</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
</project>
Now the project(1) will refer to the project-2 artifact as a dependency with scope compile:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>project1.group.id</groupId>
<artifactId>project-1</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>project2.group.id</groupId>
<artifactId>project-2</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
Note: Update the group and artifact IDs with ones you are using.

Related

unable to resolve Maven dependency placeholder reference value

my project has dependencies on some framework jars:
<groupId>my-compamy</groupId>
<artifactId>my-project</artifactId>
<version>1.0</version>
<dependency>
<groupId>my-company</groupId>
<artifactId>child-jar1</artifactId>
<version>2.0</version>
</dependency>
recently the framework team changed their packaging, in the repository, they put a parent pom (used to be separated child jars) like this:
<properties>
<reivison>2.0</revision>
</properties>
<groupId>my-company</groupId>
<artifactId>framework-parent</artifactId>
<packaging>pom</packaging>
<version>${revision}</version>
<modules>
<module>child-jar1</module>
</modules>
in the child-jar1 pom:
<parent>
<artifactId>framework-parent</artifactId>
<groupId>my-company</groupId>
<version>${revision}</version> <!-- can find the value! -->
</parent>
<artifactId>child-jar1</artifactId>
<packaging>jar</packaging>
When I build my-project, I got error message like:
... Failed to collect dependencies at my-company:child-jar1:jar:2.0.
... cuased by: could not find framework-parent:pom ${revision}
and the child-jars are not downloaded.
it looks that it is the placeholder in the child pom caused the problem: my-project could not resolve the placeholder value in the dependent(chiild-jar1)'s pom.
tried multiple approaches to resolve the referenced value from my-project side but nothing worked. Can this be resolved from my-project side without altering any dependency framework's setting? Please help.
There is a way to replace the palceholder with concrete value: add flatten-maven-plugin in the framework parent pom file build section (first to run) - this does not impact the framework's functionality but has to be done in the framework side. I gave this suggestion to the framework owner and they took it. It works.
Thanks to all comment providers!

How to import all provided Wildfly libraries in my maven project?

Is there a way to translate this human language in an xml codeblock that Maven will happily understand?
Hey Maven, look you are a great dependency management system. I am working on a JavaEE project which is intended to be deployed on Wildfly 10.1.0. Please put all Libraries that are specified in Wildflys parent BOM http://repo1.maven.org/maven2/org/wildfly/wildfly-parent/10.1.0.Final/wildfly-parent-10.1.0.Final.pom on the compiletime classpath and consider them as provided by Wildfly at runtime. And please dont bother me to list every single referenced artifact in the dependencies section of the projects pom file. Thank you Maven, you are so cool.
To clarify:
As far as I understand, importing the bom file in the dependencyManagement section of my pom file will only spare me to specify the Version Number of every single artifact, but I will still have to declare every artifactID and groupID.
This is indeed discussed here How to use BOM file with Maven
But in this answer is also stated:
Then you do not have to specify the version attribute of a dependency.
I would prefer to declare only that I am using wildfly and then be able to use all client libraries without declaring any other dependencies.
But I must admit, I have the feeling to miss something obvious. There should be an easy way to do this.
If you want everything in a another pom to be set as a dependency and as provided you can specify that in your pom. A minimal pom for wildfly 10.1.0.Final that includes everything seems to be as follows:
<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>wft</groupId>
<artifactId>wft</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>wft</name>
<description>wft</description>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-spec-api</artifactId>
<version>10.1.0.Final</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
But I would still recommend doing it the way that wildfly does it themselves, which is to put the BOM in the depencency management section and declare artifacts as you need them. Why? I don't know other than it's cleaner to read and maybe easier for maven/java to compile and build.

Jenkins fails to build multi-module Maven project

I have a multi-module Maven project where I have multiple micro services as modules so I have modules listed in my parent pom.xml like below:
<modules>
<module>core</module>
<module>model-base</module>
<module>module1</module>
<module>module2</module>
...
<module>module5</module>
<module>module7</module>
<module>module6</module>
</modules>
Here the module7 is dependent on module5, 6 so I have dependencies listed like below in my module7 pom.xml:
<parent>
<artifactId>pojectA</artifactId>
<groupId>com.domain</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>module7</artifactId>
<dependencies>
<dependency>
<groupId>com.domain</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.domain</groupId>
<artifactId>module5</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.domain</groupId>
<artifactId>module6</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
When I run mvn clean package in my local the module5, 6 called before the module7 as expected but in Jenkins it is trying to build module 5 then module7 making build fail saying:
[ERROR] Failed to execute goal on project module7: Could not resolve dependencies for project module7:jar:1.0-SNAPSHOT: Could not find artifact module6:jar:1.0-SNAPSHOT -> [Help 1]
Do I need to run any other jobs or re-order the modules in my pom.xml, how is it differ from local to Jenkins? Appreciate any help on this.
The order of modules is not relevant. Maven recognizes which project depends on which other project(s) and sets the build order in the reactor accordingly. See POM Reference, Aggregation (or Multi-Module):
You do not need to consider the inter-module dependencies yourself when listing the modules, i.e. the ordering of the modules given by the POM is not important. Maven will topologically sort the modules such that dependencies are always build before dependent modules.
Add Pre-Step as per below attached screenshot. This will compile all your top modules.
Then we can execute which ever module we want.
As is probably quite well understood, the issue is that the dependencies between the child modules fail because they aren't installed in the local repository yet (because they are yet to be built). The goal that causes this (for me anyway) is mvn test, which is invoked by mvn package. Your local build probably works because at some point you've done a mvn install and this has bootstrapped your system.
In Jenkins the only way I've found to make these builds work is to use the Pre-build step invoking a Maven target of install, and then build the main step as usual.

Maven copy parents declared in the POMs

My final goal is to create a Maven repository in a certain directory containing only a certain set of artifacts and all their dependencies.
For this I use the following command:
mvn.bat dependency:copy-dependencies -f dependencies.pom
-DoutputDirectory=localRepoDir -Dmdep.useRepositoryLayout=true
-Dmdep.copyPom=true -Dmdep.addParentPoms=true
dependencies.pom being:
<project>
<modelVersion>4.0.0</modelVersion>
<description>Dependencies</description>
<groupId>com.dummy</groupId>
<artifactId>dummy</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.dependency1</groupId>
<artifactId>dep1</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.dependency2</groupId>
<artifactId>dep2</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
When doing this, I notice that parents declared in the dependencies' poms are not copied from the .m2 Local maven repository to the destination directory.
Perhaps I'm missing something and there's a better way to do this, since it's kind of a hack to use a pom file to declare the artifacts I want to copy (together with their dependencies).
Turns out that maven was using version 2.8 as default for the dependency plugin. When explicitly indicating it to use the latest version (2.10), it worked just fine.
The addParentPoms parameter was already introduced on 2.8 for copy-dependencies, so I guess it must be a bug in the 2.8 release.
mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:copy-dependencies

How to generate Javadoc for Maven Dependencies

I have a maven project with the following POM snippet:
<modelVersion>4.0.0</modelVersion>
<artifactId>Foo-Deploy</artifactId>
<name>Foo-Deploy</name>
<packaging>pom</packaging>
<description>foobar</description>
<dependencies>
<dependency>
<groupId>de.foo.bar</groupId>
<artifactId>some-api</artifactId>
<version>${project.version}</version>
<classifier>doc</classifier>
<type>zip</type>
</dependency>
</dependencies>
The idea is to have a dependency defined in which some sources are (this is created successfully before).
Now I want to run javadoc on exactly THIS dependency. When I call
mvn javadoc:jar -DincludeDependencySources=true -DdependencySourceIncludes=de.foo.bar:some-api:*:doc:zip
it fails with the message
Not executing Javadoc as the project
is not a Java classpath-capable
package
what is wrong ? and would it work anyhow ?
or how can I generate javadoc from a specific dependency (assuming this project has more dependencies) ?
Thanks
To generate javadoc for dependent sources, a sequence of steps needs to be done. These are outlined in this link.
Essentially you need to ensure that the source files of the dependency is generated/available and <includeDependencySources> parameter is enabled.

Resources