how to handle maven internal dependancies? - java

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

Related

Dependency error in eclipse project making use of Maven

I am using Eclipse Mars to run my Java project. I am making use of Maven in it. But while trying to compile my package I am getting the following error.
Failed to execute goal on project apex-check: Could not resolve dependencies for project org.fundacionjala.enforce.sonarqube:apex-check:jar:1.0: Failed to collect dependencies at org.fundacionjala.enforce.sonarqube:apex-squid:jar:1.0: Failed to read artifact descriptor for org.fundacionjala.enforce.sonarqube:apex-squid:jar:1.0: Could not transfer artifact org.fundacionjala.enforce.sonarqube:apex-squid:pom:1.0 from/to central (https://repo.maven.apache.org/maven2): Failed to authenticate with proxy -> [Help 1].
I am able to find that my pom.xml has a bug in its dependency. But don't know how to resolve it. I have given below my pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!-- ~ Copyright (c) Fundacion Jala. All rights reserved. ~ Licensed under the MIT license. See LICENSE file in the project root for full license information. -->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.fundacionjala.enforce.sonarqube</groupId>
<artifactId>apex</artifactId>
<version>1.0b${build.number}</version>
</parent>
<artifactId>apex-check</artifactId>
<name>Apex :: Checks</name>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apex-squid</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
I have written 'apex-check' and 'apex-squid' as two separate projects.
Can anyone explain how to correct my pom.xml?
You need to have:
org.fundacionjala.enforce.sonarqube:apex-check:jar:1.0
org.fundacionjala.enforce.sonarqube:apex-squid:jar:1.0
jar files available in your local .m2/repository folder. If they are not present maven will try to download from central repository and as an expected result it will fail on firewall or it will not find the artifact. i.e. if:
apex-check requires apex-squid project as dependency first you need to install squid project files by using mvn install on squid project folder.
But it seems more like you want to create a multi module maven project, take a look at this question. Create a parent project similar to this project, add check and squid as module and run mvn clean install on parent.
**edit: I just see you already have parent, so make sure parent has your projects as modules, which helps reactive build order and eclipse imports

could not find or load main class org.tesng.TestNG

I am using testng-6.8.21.jar for writing test case from the following link:
http://www.tutorialspoint.com/testng/testng_tutorial.pdf
I am able to compile the java file TestNGSimpleTest.java
but when I try to use this command:
java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
it says:
could not find or load main class org.tesng.TestNG
You must provide full path to the jars in the classpath. For example :
java -cp '/path/to/testng-6.8.8.jar' org.testng.TestNG testng.xml
But testng requires other dependencies that you also must include in the classpath :
\- org.testng:testng:jar:6.1.1:test
+- junit:junit:jar:3.8.1:test
+- org.beanshell:bsh:jar:2.0b4:test
+- com.beust:jcommander:jar:1.12:test
\- org.yaml:snakeyaml:jar:1.6:test
The easiest way is to use a dependency manager. For example, Maven.
Briefly, you need (not required but it makes everything easier) to have a standard project structure :
main-directory
pom.xml <- File required by maven. It always has this name.
-src
-main
-java <- Place your Java classes here
-resources <- Place your images, conf files here etc.
-test
-java <- Place your java test classes here
-resources <- Place your test resources here.
Then, with this simple pom.xml, Maven understand that you want testng and downloads testNG's dependencies :
<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.mycompany</groupId>
<artifactId>my-app-name</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!-- Declare your dependencies here-->
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Then launch :
mvn test
And if you want to have a view on the dependencies, use :
mvn dependency:tree
(This is how I got the preceding dependency tree)

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'

How te rename EAR artifact with Maven

I have a project Maven ear modules, and i like to rename the ear from ProjectIt-4.1.0.ear to ProjectIt-4_1_0.ear in fact to have a version like that : x_y_z to resolve a deployement contraints
You can define a custom property holding your key with your own separtor between major, minor and maintenance version.
Then you can use that property in the build name of your artifact:
<project>
...
<properties>
<custom.version>x_y_z</custom.version>
</properties>
...
<build>
<finalName>${project.artifactId}-${custom.version}</finalName>
</build>
</project>
Meanwhile, I wouldn't advice such a method of shipping artifacts version, because it would not fit the common version syntax. You can read more about Semantic Versionin in semver.

Multi-Modules maven pom

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.

Categories