Maven project not working with other projects - java

I have a Java project that uses http common-logging.jar, httpclient-4.4-alpha1.jar, httpcore-4.4-alpha1.jar, httpmime-4.4-alpha1.jar. I converted my Java project to a Maven project and added the above jar files to my pom.xml. This is my XML file:
<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>MyClient</groupId>
<artifactId>MyClient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
</build>
</project>
I did Run As > Maven Clean and Run As > Maven Install and it built the project. It generated a jar file in the target folder called MyClient-0.0.1-SNAPSHOT.jar. I added this jar to another project that uses this jar file (to make HTTP calls) and I get the error
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/http/entity/mime/content/ContentBody
Can someone tell me what is causing this? This seems to like a dependancy issue? Can someone tell me what I am doing wrong?

Maven will not automatically package these dependencies into your jar file. These files are only used to compile the code and build the jar file itself. As the documentation for the jar plugin states:
The resulting 'jar' file contains the compiled java class files as well as the files from src/main/resources.
You have a number of options:
Package your MyClient jar file as a "fat" jar using something like the maven shade plugin
Add the dependencies from the MyClient POM to the POM in the project that uses the jar file.
Use a maven multi-module project with a common parent so that transitive dependencies such as this are resolved between the projects.
Manually copy all of those jar files over when you copy the MyClient jar file

Related

Java 8 to Java 9 migration optimal way for mavenised project

I am migrating my project from Java 8 to Java 9. My project is mavenised. Now for migrating to Java 9, I am planning on creating a separate module directory where all the required dependency of a module will go.
For doing this through maven the only way I know is to use the copy plugin of maven to copy all the required dependency in the module directory. So after running maven installs for a module, the dependent jars will be copied in repository folder(which is by default) and also copied to this module directory folder.
So there will be a copy of the jars and also hard coding in pom.xml for copying specific dependency in the module directory.
This approach doesn't seem to be clean, is there any way out were automatically maven can read my module-info.java file and copy the required dependency not in the classpath but in the specified directory
Here is my pom.xml
<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>
<parent>
<groupId>com.aa.bb</groupId>
<artifactId>cc</artifactId>
<version>0.0.1</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>dd</artifactId>
<name>dd</name>
<groupId>com.aa.cc</groupId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<release>10</release>
<compilerArgs>
<arg>--module-path</arg>
<arg>./moduledir</arg>
</compilerArgs>
</configuration>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>6.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
module-info.java
module com.some_module_name {
requires com.fasterxml.jackson.core;
requires com.fasterxml.jackson.databind;
requires org.apache.commons.codec;
requires spring.beans;
requires spring.context;
}
After adding module-info.java to an existing java 10 module and running maven i am getting issue like "package exist is used in module a and module b".
What i believe is while running maven it is looking for module dependency in the .m2/repository and there are loads of jar there as the m2./repository is common for my multiple modules.
So what i was planning to do is create a separate module directory for each module and place the required jar for that module in it which way it even works.
I assume you're making this effort to make sure Maven "does the right thing" regarding the module and class path, i.e. placing direct dependencies of your module on the former and all other dependencies on the latter. If that's so, there's nothing you need to do - from version 3.7 on, the Maven Compiler Plugin does it for you as soon as you add a module-info.java to your src/main/java directory.
You can verify that by running Maven in debug mode with mvn clean compile -X. When you carefully analyze the output, you will see which JARs end up on which path.

Not able to find artifact jackson-modules-java8 jar

I am using pom.xml like
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
but there is no .jar available in public repo. It have only .pom file in respective release. Public maven repository link
How to get .jar for it.
You can manually download .zip for respective release from jackson-modules-java8/releases
and build it locally using command.
mvn clean install
It will give you .jar for each module under respective target directory.
You need to also include that dependency in your pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-modules-java8</artifactId>
<version><!-- same version as your other jackson dependencies --></version>
</dependency>

Compile subset of dependencies into my jar file

I have a project with multiple Maven dependencies and want to minimize the size of my compiled JAR. Right now it seems that IntelliJ is compiling all of the external dependency source files into my JAR even though I only use a small subset of their functionalities.
I would like to include only the files that are directly used by my module.
You can declare a dependency as optional:
<dependencies>
<dependency>
<groupId>sample.dependency</groupId>
<artifactId>small-dependency</artifactId>
<version>1.0</version> <!-- Will be packaged in JAR -->
</dependency>
<dependency>
<groupId>sample.dependency</groupId>
<artifactId>really-big-dependency</artifactId>
<version>1.0</version>
<optional>true</optional>
</dependency>
</dependencies>
Another approach is to use the provided scope. The difference is provided is used if you know the dependency will be included in the classpath of the application that will run your JAR (e.g. a Web or Java EE container):
<dependencies>
<dependency>
<groupId>sample.dependency</groupId>
<artifactId>small-dependency</artifactId>
<version>1.0</version> <!-- Will be packaged in JAR -->
</dependency>
<dependency>
<groupId>sample.dependency</groupId>
<artifactId>really-big-dependency</artifactId>
<version>1.0</version>
<scope>provided</scope> <!-- Will not be packaged in JAR, needs to be provided in classpath at runtime -->
</dependency>
</dependencies>
Sources:
Maven - Optional Dependencies and Dependency Exclusions
Maven - Dependency Scope

Maven inherit dependencies from a project into another

Given a Maven project A producing a jar:
<groupId>myprojects</groupId>
<artifactId>A</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>some.needed.group</groupId>
<artifactId>some.needed.artifact1</artifactId>
</dependency>
<dependency>
<groupId>some.needed.group</groupId>
<artifactId>some.needed.artifact2</artifactId>
</dependency>
</dependencies>
and a project B depending on A and producing an enterprise archive:
<groupId>myprojects</groupId>
<artifactId>B</artifactId>
<version>1.0</version>
<packaging>ear</packaging>
<dependencies>
<dependency>
<artifactId>A</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
how can I achieve to have the project B ear artifact to include not only the project B's jar artifact but also its dependent artifacts some.needed.artifact1 and some.needed.artifact2?
I edited and added this because it seems that the behaviour occurs only when running mvn from Bamboo:
The strange thing is that it is working when I'm running mvn locally from Eclipse but not when it's being ran by the Bamboo continuous integration server. On a local run I have the application.xml generated correctly with references to all the transitive dependencies the corresponding jar files copied to the lib folder. On Bamboo plan the generated application.xml does not include the references and the jar files are not in the lib folder.
First as a correction I think dependency should be on A, not on B , like this :
<dependencies>
<dependency>
<artifactId>A</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
And about the original question of having the dependent jars , i think they should be available in your EAR file , as you have dependency on A project and which in turn depends on some.needed.artifact1 and some.needed.artifact2(transitive dependencies) as the scope you provided is default so maven will take it as compile and all the jars will be bundled in your ear file.
Thanks
In project B which produces the ear, you can include a dependency to A like this
<modules>
<module>project A</module>
</modules>
<dependencies>
<dependency>
<groupId>some.needed.group</groupId>
<artifactId>A</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
In project A, define the parent project to be project B like the following
<parent>
<groupId>myprojects</groupId>
<artifactId>B</artifactId>
<version>1.0</version>
<name>project B</name>
</parent>
<name>project A</name>

Missing jar from repo1.maven.org

Eclipse is unable to find and download the following jar:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcomponents-client</artifactId>
<version>4.1.2</version>
<packaging>pom</packaging>
</dependency>
http://search.maven.org/#artifactdetails|org.apache.httpcomponents|httpclient|4.1.2|jar
http://repo1.maven.org/maven2/org/apache/httpcomponents/httpcomponents-client/4.1.2/httpcomponents-client-4.1.2.jar
Resolved: I dunno where the pom came from.
Is missing.
It's not missing.
That's a pom (Project Object Model) file.
You probably want the httpclient jar. The pom describes the total project (multiple jar files).
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.2</version>
</dependency>

Categories

Resources