This question already has answers here:
Can I add jars to Maven 2 build classpath without installing them?
(24 answers)
Closed 6 years ago.
I've simple Eclipse Web Project based on Adobe framework.
Now I have to upgrade my project to a new version of framework and I wish to use Maven to manage dependencies, packaging, ecc.
The problem is that in the Adobe documentation is write that I MUST use thier own jars that are a lot (69). I see Maven System_Dependencies, but i'm looking for something smarter that add all 69 entries like that.
Usually, I would create an Eclipse UserLibrary and would add to Eclipse BuildPath, but in this case I don't know how to add them to maven classpath to compile and package the project properly
To include external jars you can add the dependency with System scope.
<dependency>
..
<scope>system<scope>
<systemPath>your jar path</systemPath>
</dependency>
Also you can define your compiler plugin to include the directory in your classpath.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<includes>
<include>directory path/*.jar</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
NOTE: This will make your build success. But not sure how to make those classes available for access inside IDE.
Related
This question already has answers here:
Add external library .jar to Spring boot .jar internal /lib
(8 answers)
Closed 4 years ago.
When I mvn clean package the project and run it with java -jar target/project.jar it throws an error that it cannot find some class from an external jar.
Steps taken
All external jars are in a folder of my project: /jars or /jars/morejars/
Adding the jars to the build path: In Eclipse I right click on project, go to build path and select add external archives. I can see that eclipse creates a library folder "referenced libraries" below the "Maven dependencies" folder. When I check project -> Properties -> Java Build Path -> Libraries I can see all the imported jars.
Some of those external jars are described as <dependency> (with <systemPath) in pom.xml, so they will not be seen in Referenced Libraries but in Maven dependencies (interestingly, the class that cannot be found when running my packaged project is in an external jar that doesn't reside in Referenced Libraries but Maven dependencies)
e.g.
<dependency>
<groupId>external.jar.groupid</groupId>
<artifactId>external.jar.artifactid</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/jars/external-jar.jar</systemPath>
</dependency>
Use the maven assembly plugin (as described here), this is my full <build> config:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
Run mvn clean package -> BUILD SUCCESS
java -jar target/project.jar -> Cannot find some.class from external.jar
Thanks for the help
The problem is clearly with dependencies. While your application starts loading it looking for required classes to run properly. While it loading a class from a jar library and that library class also refer to some other class from a another jar library file but that library (jar file) is not available in your class path, it will give you the above error. So check for maven dependency and get all the jars required by your application. Also based on error message also you can add the jar reference in your pom.xml file. For example if you get an error like Failed to load com.apache.some.Example.class just google the Example.class jar file and get it from maven repository.
Hope this will help you.
This question already has answers here:
How can I create an executable/runnable JAR with dependencies using Maven?
(33 answers)
Closed 6 years ago.
I have several java non-maven projects (with one main project) and several maven projects (with one main project). Now I need to use the maven-projects functionalities in the non-maven projects.
I have to say, I know very little about maven. And I'm working in NetBeans IDE.
There are several options I've come up with:
Make non-maven projects maven projects and add dependencies.
I cannot do that because others use the non-maven projects their way and I cannot just make changes like this.
Make maven projects non-maven projects and add them as Libraries
I cannot do that because there would be a lot of libraries to add. The dependencies might be large.
To non-maven projects add jars as libraries of the maven projects. This is same as the (2) option. I tried it and added all the maven project jars as libraries to my non-maven main project, but at a run-time there was a lot of NoClassDefFoundError exceptions because of missing jars (3rd party jars that the maven projects depends on).
?
Any ideas?
Thank you in advance.
Solved
I used the pom adjustments from chresse.
The whole pom is here: https://codeshare.io/5ZeYN2
I used the maven command from the question Tunaki marked this was duplicate of (how-can-i-create-an-executable-jar-with-dependencies-using-maven)
mvn clean compile assembly:single
Thank you all.
you can generate a jar from your maven project including all dependencies.
add the following plugin to your pom of your main maven project. the mvn assembly:single will create a additional jar ('projekt'-jar-with-dependencies.jar) with all dependencies, which can be included in your non-maven project:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Using maven-compiler-plugin I specified three packages to be ignored from source. However, after importing the project into IntelliJ, those folders are treated as source packages normally, and this happens after every POM reorganization. Needless to say, I have to exclude them manually after every POM change.
I know there was some maven-idea plugin, but I see it has been retired. I use IDEA 13.
How to make IntelliJ IDEA to support packages exclusion based on Maven?
Relevant pom excerpt:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>2.3.2</artifactId>
<configuration>
<excludes>
<exclude>test/**/*</exclude>
<exclude>unused/**/*</exclude>
<exclude>my/test/package/unused/**/*</exclude>
<excludes>
<configuration>
</plugin>
Works fine for Maven build (classes from excluded packages do not compile).
I am currently using the maven-jar-plugin to build a JAR from a specific class folder which I have included as a provided scope dependency. I now need to include this JAR as a dependency in my final build.
So, is it possible to include a dependency at a specific phase of the build?
NOTE: The dependency is built during the build process, so it is only available during the compile phase.
The simplest way to tackle this would be to separate the code you are building with the maven-jar-plugin into a separate Maven project, and just refer to it as a normal dependency in the main Maven project.
This way you don't need to mess around with having one Maven build produce two different artifacts (the JAR you are generating and then the main output minus that JAR's classes) while having the output of the first being referred to in the second.
Alternatively, assuming your main project has the package type set to jar, you could just add the "specific class folder" classes to the project's <resources> element and then configure/override the maven-jar-plugin execution that runs during the package phase to exclude these classes, with the <excludes> element in the <configuration>.
You could configure the files included in the generated jar :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<includes>
<include>relative_path_to_your_jar</include>
<include>**/**</include>
</includes>
</configuration>
</plugin>
http://maven.apache.org/plugins/maven-jar-plugin/jar-mojo.html#includes
How can I change libraries on Eclipse with Maven builder?
When I change my JRE library by the project properties and then rebuild a project JRE returns to previous version.
How can I add a new library?
A library is just a dependency. You can add dependencies to you pom-file:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
This add the junit-library.
When I change my JRE library by the project properties and then rebuild a project JRE returns to previous version.
This is the case with m2eclipse/m2e, as the Eclipse plugin will revert any changes made to the project properties. In most cases, you're better off specifying the version of the source code, and the target version of the bytecode, via the maven-compiler-plugin configuration:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin<artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
M2Eclipse/M2E uses these values in the project POM to determine the Java runtime to use for building the project. Considering that it never makes sense (or is impossible) to have multiple Java runtimes for an Eclipse project, you ought to specify the source and target values for the project, either in the project POM or in a parent POM.
If you are using Maven, then you control all your dependencies via the pom.xml file. You can either use the maven eclipse plugin to generate the eclipse artifacts, or my preferred approach which is to use the m2eclipse plugin. This plugin makes it easy to keep maven and the eclipse workspace in sync.