Implement project with maven in another maven project - java

I have created a Java project with maven that I want to implement in other maven project.
The child project is not a spring project, is a Java project created from scratch migrated to maven to manage the dependencies.
I can generate the jar with mvn install. So far so good.
Then I try to implement this jar to other project that is built with maven and spring.
For that I added that jar to my local maven repo with the following command:
mvn install:install-file -Dfile=/Users/.../target/app-0.0.1-SNAPSHOT.jar -DgroupId=com.mycompany.childapp -DartifactId=childapp -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar
Then I am able to see that in the repo folder in .m2, the jar file is correct.
And this in the parent project pom.xml:
<dependency>
<groupId>com.mycompany.childapp</groupId>
<artifactId>childapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
And also, in the plugin section:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
At least for eclipse everything is good, as I can develop and implement the classes inside the library without any issue. I can build the parent app with mvn clean package But when I run the app and try to execute the section of the code that uses the child library I get ClassNotFoundException for the classes that are in the child project.
In the parent project, in the libraries imported by maven I see that my library has a folder icon instead of the one the other have.
How could this be fixed?

mvn clean package creates jar for parent project but it does not include child jar.
You should use SpringBoot
mvn bootRun to run your project locally
mvn bootJar to build fat jar which will include all required dependencies.
To run this fat jar call java -jar parent.jar

Finally it was that the library was like... 'corrupted'.. or something like that.. It was a Java Project (created with eclipse wizard) than then I converted to maven (with eclipse wizard).
I started a new project as maven project from the beggining and everything was working at the first attempt....
So I just have to run mvn clean install to generate the jar and installed in the .m2 repo
Then in the main app pom.xml I have to declare as any other dependency:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>mylibrary</artifactId>
<version>0.0.1</version>
</dependency>

As per the comments, you want to create a project with maven which will create a jar file. That jar file will be used in another project. If my understanding is correct. You have to do the followings.
Create a simple maven project with simple type archetype.
Run the pom.xml with the command like mvn clean install or mvn clean package.
Use the jar file from the simple project upload into your nexus/artifactory repository and use the maven definition in another maven project.
If you do not have nexus or artifactory, you can use the jar file locally in another maven project.
To add a jar file locally in another project, you have to add the following dependency structure in pom.xml.
<dependency>
<groupId>your group id</groupId>
<artifactId>name of the application</artifactId>
<version>some version</version>
<scope>system</scope>
<systemPath>your location/jar-file-name.jar</systemPath>
</dependency>
Hope it answers your question.

Related

Maven project architecture

I am trying to add the locally created project which contains JARs added using java build path to my maven project as dependency and the JARs used in the dependency project are also be useful in the main project. how to deal in this scenario if I don't want to convert my dependency project to maven?
You have different possibilities:
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>2.53.1</version>
<scope>system</scope>
<systemPath>
C:/.../your-jar.jar
</systemPath>
</dependency>
or you can install it in your local .m2 repo
https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
This doesn't force you in any way to convert your project into a maven project, but let you reuse the jar you already have.
This is similar like Maven multimodule project. In that all the submodules gets added to the root project as a jar after successful compile. In your case try covert the project into jar and copy the same jar file to the lib folder of parent project.

How to organize local dependencies with Maven?

in my company, I have a Java project:
common: This contains classes that are used for several projects
GUI: This project contains a GUI for common
Without Maven, I add common to the class path, I can build the project. But how to setup the POM.xml and the Eclipse workspace when I want to build the GUI with Maven?
I also want to package the app into a JAR later, so I tried to setup the gui as .jar package
But then how to assign a Maven type to the common project?
Ideally, I could import "common" to the Maven project?
UPDATE:
Ok, it seems the mvn package command is able to resolve the "common" project when I add as local dependency see this under GUI. Still a bit confused about whether to use "pom", "maven-plugin" or "module" - anyone can add some links/clarifications, when to use what approach?
Declare common as usual maven dependency in GUI.
If it isn't maven project, add it to local repository as shown there How to add local jar files in maven project?
I would follow this steps
Create a local maven repository to store your custom jars. Which nothing but a proper directory structure with pom files in it.
Create your sub projects(common, gui) and package them as jar.
Install the local jars to local mvn repository.
Use the jars as dependency from your project.
Example:
Create a master project on ${master_project} location and your subprojects on ${master_project}/${common} and ${master_project}/${gui} location.
Create mvn repository in: ${master_project}/local-maven-repo
Specify your local repository location in your subprojects pom.xml located in ${master_project}/${gui}/pom.xml
<repository>
<id>local-maven-repo</id>
<url>file:///${project.parent.basedir}/local-maven-repo</url>
</repository>
Install your jars in local mvn repository
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
Where: <path-to-file> the path to the file to load
<group-id> the group that the file should be registered under
<artifact-id> the artifact name for the file
<version> the version of the file
<packaging> the packaging of the file e.g. jar
Finally use them as regular dependency
<dependency>
<groupId>org.company</groupId>
<artifactId>common</artifactId>
<version>0.9.0-SNAPSHOT</version>
</dependency>
Reference for this answer.
Here's further resources for Maven multiple module projects
install the maven plugin in your eclipse using the following link.
This will enable maven in your eclipse. Then right click on your project and click Configure and you will see Convert to Maven Project option. This will convert your existing project to a maven project. You can configure your project to be build as a .jar, .war etc. while converting from the wizard. The following is the tutorial on how to do it... link

how to add jar dependency in maven?

When I run
mvn compile
I get package com.ibm.icu.util does not exist. So I downloaded the ICU4J jar and installed it into the local repository. I confirmed it's in .m2/repository/com/ibm/icu/icu4j/3.4.4/icu4j-3.4.4.jar. Inside that jar file is the missing class file com/ibm/icu/util/Calendar.class. Then I added the following into the dependencies section of pom.xml:
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>3.4.4</version>
</dependency>
But when I run mvn compile again, I get the same error. What am I doing wrong?
You should avoid adding dependencies manually.
If you don't know a groupId and artifactId of the dependency you need, search for it at http://mvnrepository.com/. Usually, groupId matches the package names in the jar file.
For your case, the dependency is already there: http://mvnrepository.com/search?q=com.ibm.icu
So, go to http://mvnrepository.com/artifact/com.ibm.icu/icu4j and get the version of the dependency you need, e.g. 55.1: http://mvnrepository.com/artifact/com.ibm.icu/icu4j/55.1
Grab maven dependency xml and put it to your pom.xml file:
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>55.1</version>
</dependency>
If you didn't find your dependency try to find it in google. Sometimes the dependency may be found in some corporate public repositories, not in a central. In this case you need to add the third-party repository to repositories section of your pom.xml.
If you're unable to find your dependency in the public repository then you have three options:
A. Install jar to internal repository server (e.g. nexus)
B. Put the JAR file in your project sources and declare project maven repository :
<repositories>
<repository>
<id>my-local-repo</id>
<url>file://${basedir}/my-repo</url>
</repository>
</repositories>
Important: You should keep the maven repository layout in your local repository.
C. [Bad Practice] Use maven install plugin to install your custom jar to local repository on your machine. But it's a badIt's not recommended.
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=path-to-your-artifact-jar -DpomFile=path-to-pom
D. [Bad Practice] Use system dependency with absolute path to the JAR file, although it's a bad practice and should be avoided.
<dependency>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>X.Y.Z</version>
<scope>system</scope>
<systemPath>${user.home}/jars/my.jar</systemPath>
</dependency>
You should not be manually installing things into the maven repository directory. That directory is maintained by maven itself.
The way dependencies work is that when you run mvn compile or some other goal, it will connect to the maven central repository and download the needed dependencies and their dependencies.
If you manually install a jar file, it may not have it's dependencies. That icu artifact will likely have other things it depends on. Maven will automatically resolve these dependencies.
I would recommend using mvn clean install this will clean the target directory and rebuild everything.
If it fails to download, then you likely need to change the maven configuration. For example, if you are behind a proxy server you need to configure maven with the proxy credentials.
Also, if you manually copied anything into the .m2/repository/ directory you should delete it and let maven put it in there correctly instead.
The beauty of maven is that you don't need to worry about things like downloading jars. It just handles that for you. Let it do it's job.
If you have an internal artifactory like JFrog maybe you should check that the jar is there. Do not download manually to .m2 because it's at least strange. At most you can upload the jar in that artifactory manually.

Maven won't load my custom library

I have two projects both use maven. The project A is a utility library, and I use mvn install to my local maven repo. The project B is a web app, it has a dependency on A library.
when I run B project use mvn jetty:run it don't load a.jar in my local maven repository, but if I execute mvn package to package my web app, the a.jar will in its WEB-INF/lib.
I tried under maven 3.1 and maven 3.2, both have the problem.
I'm new to Maven 3 and have been using Maven 2 for many years, why can't this solution work as before?
Edit:
From the pom.xml:
<dependency>
<groupId>me.donnior</groupId>
<artifactId>rtl</artifactId>
<version>0.3</version>
</dependency>
Edit:
My problem is that my own installed A.jar can't be found when run mvn jetty:run, but others third-party jars that downloaded from network can be found in class path.
Updated:
I figure it out is a jetty plugin's dependency problem, can be closed now.
mvn jetty:run dont perform packaging Lifecycle.
It enables project without requiring that it is packaged. In this mode every component of the project is used from its origin location.
try to use jetty:deploy-warinstead as it perform mvn package lifecycle phase as well.

Intellij not importing updated jar

I have a maven project and I have created a new external jar called myjar. I subsequently updated my pom to myjar.17.1.snaphot from myjar.17.snaphot. In my external libraries, I do not see this jar - I still see myjar.17.snaphot. Therefore, Intellij cannot compile my code.
Whe I do a maven install at the command line, all works fine. Why doesn't intelliJ update my jar?
<dependency>
<groupId>com.me</groupId>
<artifactId>myjar</artifactId>
<version>17.1-SNAPSHOT</version>
</dependency
IntelliJ can not be aware of a change to an external dependency. When you change your version in an external dependency, there is no JAR installed into your local repo ($HOME/.m2). Once you do mvn install there is a file in this dir, IntelliJ notices it and adds it as a dependency.
A possible workaround is to add this external dependency to a parent pom as a module of your project.
EDIT
After installing you also need to update all references in your project poms to the new snapshot version, then right click on the pom.xml in Idea -> Maven -> Reimport.

Categories

Resources