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.
Related
I am using the Eclipse IDE and in the past, I have been developing a maven project depending on local sources (from github (sap-olingo)) as well as maven dependencies loaded as jar. I have now updated the github source and also all the dependencies which seems to break standard eclipse export functionalities (right click porject -> export -> war)
The webproject is working fine when using the maven install functionality and is loading all sources into the META-INF/lib classes, but when using the export as war from eclipse (which I did previously) it is missing to compile a proper dependency. Missing in the sense of that the jar is still present in the META-INF/lib directory of the war, but the content is empty and the size is only 1kb
When comparing the mvn clean install to the exported, there are rarely any differences. Except for the projects that are to be compiled during the runtime. Sizes of the libs in the mvn created war and the exported war roughly matches except for that one jar.
Is there a way to root cause that issue? I am using solely the maven integrated into the IDE which is why I doubt differences in the maven versions and or jdk.
Unfortunately I have no idea in where to start and what info you require
Thanks for your input
EDIT:
My projects pom:
<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>com.test</groupId>
<artifactId>odata</artifactId>
<packaging>war</packaging>
<version>4.0.0</version>
<name>odataMaven Webapp</name>
<url></url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sap.olingo</groupId>
<artifactId>odata-jpa-processor</artifactId>
<version>0.3.7-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.2</version>
</dependency>
</dependencies>
<build>
<finalName>odata</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${project.build.source}</source>
<target>${project.build.source}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.source>1.8</project.build.source>
</properties>
</project>
The odata-jpa-processor source/project is pulled from here. The problem exists solely with the olingo-jpa-metadata jar which is empty. All other maven dependencies are exported correctly.
I have Java Maven project in Eclipse. When I do maven build and set goal to package maven builds jar file with name my-project-0.0.1-SNAPSHOT.jar. I need package to be always constant name since I put it system that starts it with script. my-project.jar would be fine. How to achieve that?
How to ask maven to put all jar libraries my project is using into my-project.jar?
How to ask maven to place my-project.jar into particular folder target/ready_release. Currently maven puts jar into target folder. How to ask maven copy all libraries and configuration files project is using into this folder too.
Maybe I'm mistaking and all these jobs should be done under some other maven goal or any other operation?
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>
<groupId>com.aaa.rfid.scaleandlabel</groupId>
<artifactId>my-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<organization>
<name>aaa</name>
<url>www.aaa.lt</url>
</organization>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.scream3r</groupId>
<artifactId>jssc</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
</project>
You can specify the name of the package by configuring the jar plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<finalName>ready_release/my-project</finalName>
</configuration>
</plugin>
This will create the jar file at <project-root>/target/ready_release/jar-name.jar and every subsequent build will overwrite it.
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
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
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>