How to organize local dependencies with Maven? - java

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

Related

Implement project with maven in another maven project

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.

How to add 3rd party JARs to Maven project [duplicate]

This question already has answers here:
Add a dependency in Maven
(5 answers)
Closed 3 years ago.
My area of expertise is not Java. I develop in other languages on other platforms.
Right now I'm developing a series of Java servlets for a project. The servlets are to run on a CentOS server running FileNetP8.
I actually got all of the planned items finished and running.
Now when I am trying to add a few more services, the library I came across is available via Maven. I have no idea what Maven is. Read up on it today. Created a test project to try it out.
My development environment is Eclipse Photon on Windows 10.
Now my problem is I can't figure out how to add the Filenet JARs to the project. I can't upload them to some Maven repo. Searching the Web says to add them to the local repo, but I don't understand how to do that to the Local repo in Eclipse's builtin Maven.
I think that the JARs don't need to be packaged within the deployment WAR because the app will be deployed to the Websphere server that runs Filenet so they should be available on it. Should I add them as external JAR references to get the project to compile?
I provide below the following approaches to check based upon your suitability.
Approach-1: Install manually
If you have only 1 jar file, execute the following command by replacing as per your requirements.
mvn install:install-file \
-Dfile=<file path location> \
-DgroupId=<your own custom group id> \
-DartifactId=<your own custom artifact id> \
-Dversion=<some version number> \
-Dpackaging=jar \
-DgeneratePom=true
Once it is done, use the following in your project pom.xml in the dependency section.
<dependency>
<groupId>your own custom group id</groupId>
<artifactId>your own custom artifact id</artifactId>
<version>some version number</version>
</dependency>
The downside of the above approach is, only you can use it as it installs in your .m2 directory. For other developers, they have to follow the same approach. It is not a suggested approach.
Approach-2: Manually add the location of jar file
Add the following dependency in pom.xml
<dependency>
<groupId>some.group.id</groupId>
<artifactId>some.artifat.id</artifactId>
<version>some.version.no</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/yourActualJarFileName.jar</systemPath>
</dependency>
The downside of the above approach is, you have to put all the jar files in a directory and you have to provide the path. All the jar files should be part of your project, it means all the jar file should be put in source code repository. Although it servers the purposes, still it is not a good approach. If you got a newer/higher version of jar file, again you have to put it inside your lib directory. It means you have manage all the old and new versions of jar files.
Best Approach
Maintain Nexus or Artifactory, or any artifactory management system in the organisation and put all the jar files and provide the definition of your custom jar file. It will provide you pom definition. Based upon this, you have to add the dependencies in your project pom.xml. Here you can maintain n number version of your custom jar files.
Seeing as every answer involves maven i'll try to provide a different and somewhat old school approach. You could always import your jars directly into your project by right clicking on it -> properties -> Add Jars. Apply when done and VoilĂ . This is far easier than understanding the complexity of maven.
Dependencies need to be added in pom.xml under dependencies tag. Maven will download the specified jars from maven central repository for the first time, and it will be saved in your local repo.
If the dependencies are not available in central repo and if they have their own repo, you to need specify it in the repositories tag.
<repositories>
<repository>
<id>repo-id</id>
<name>repo-name</name>
<url>http://repourl</url>
</repository>
</repositories>
Any change in the pom.xml, maven will automatically download it.
<dependencies>
<dependency>
<groupId>dependency-id</groupId>
<artifactId>artifactid</artifactId>
<version>1</version>
</dependency>
</dependencies>
If the jars are available in Maven Central repo then all you hvae to do is add dependency under dependencies section in your pom.xml file. Set the scope as required, like you said jars don't needed to be packaged in WAR file as they might be available on server then you can set scope to provided.
<dependency>
<groupId>groud-id</groupId>
<artifactId>artifact-id</artifactId>
<version>version-numbe</version>
<scope>scope</scope>
</dependency>
If jars are not available in central repo and you want to install them on your local repo then below command should help you. More information here.
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

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.

Maven doesn't see dependencies which are JAR files in a subfolder

I have a project where I use Maven as the build tool.
In my pom.xml file I have these dependencies:
<dependency>
<groupId>com.my.group1</groupId>
<artifactId>MyArtifact1</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.my.group2</groupId>
<artifactId>MyArtifact2</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.my.group3</groupId>
<artifactId>MyArtifact3</artifactId>
<version>1.3</version>
</dependency>
Within the folder where I have my pom.xml, I also have a subfolder, \external. In this subfolder, I have the above listed artifacts as jars: MyArtifact1-1.0.jar, MyArtifact2-1.1.jar, MyArtifact3-1.3.jar.
The problem is that when I run mvn install, I get this:
[ERROR] Failed to execute goal on project my-project: Could not resolve dependencies for project com.my.group4:sample-project:jar:20161207.3: The following artifacts could not be resolved: com.my.group1:MyArtifact1:jar:1.0, com.my.group2:MyArtifact2:jar:1.1, com.my.group3:MyArtifact3:jar:1.3: Failure to find com.my.group1:MyArtifact1:jar:1.0 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced
So far, I tied running this:
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=./external/MyArtifact1-1.0.jar -DgroupId=com.my.group1 -DartifactId=MyArtifact1 -Dversion=1.0 -Dpackaging=jar
I tried the above for all 3 packages, but still it looks like it didn't have any effect.
You are trying to include in the maven compilation .jar files you downloaded in your computer ?
Remember that Maven will try to get your dependencies from the local repository first (a folder in your computer) and from the global repositories later. If your dependencies (.jar) are not in those repositories, you will get an error message.
Here I give you some ideas/solutions:
If the .jar files resulted from other maven projects
If the .jar files are the product of other maven projects you created in your computer. You can install the outputs of that project in your local repository. Once they are installed, maven will find the dependencies locally.
// in the other projects
mvn clean install
If you have many local projects, you may setup your own maven repository to manage all your reusable components. Many companies setup their own repository in their local network. You can use software such as Artifactory
or Nexus Repository Manager. You can even built a repository using your filesystem. Note that, in order to use these repositories, you have to configure them in the pom.xml file of your project.
<project>
...
<repositories>
<repository>
<id>my-internal-site</id>
<url>http://myserver/repo</url>
</repository>
</repositories>
...
</project>
If the .jar files were downloaded from other websites
Sometimes you need .jar files that exist in global maven repositories. You may try first to search in these repositories. You can go to sites such as MVN repository to search or browse the existing packages in the repositories. If you find the dependencies you need, you can obtain too the required dependency to include in the pom.xml.
If the .jar files does not exist in any repository, you can install the file in your local repository. There is a tutorial in the Heroku's devcenter and another one in Mkyong. You must pick a groupId-artifactId-version for the file, install the file and add the dependency to the pom.xml of the project.
Note that, if the .jar was not created using maven, you must provide all the parameters
mvn install:install-file -Dfile=path-to-your-artifact-jar \
-DgroupId=your.groupId \
-DartifactId=your-artifactId \
-Dversion=version \
-Dpackaging=jar \
-DlocalRepositoryPath=path-to-specific-local-repo
If the .jar was generated with Maven, that file will include their own pom.xml with metadata. If you use Maven 2.5+, you do not need to provide the groupId-artifactId and version.
mvn install:install-file -Dfile=path-to-your-artifact-jar \
-DlocalRepositoryPath=path-to-specific-local-repo
There is another option. You can use the system scope with a local systemPath. However, I think this solution requires that people keep the library in the same location for all the developers, or requires to include the .jar file in the project/code repository. Using a repository is a better idea.
<dependency>
<artifactId>..</artifactId>
<groupId>..</groupId>
<scope>system</scope>
<systemPath>${basedir}/lib/dependency.jar</systemPath>
</dependency>
Please check whether these jars present in your local repository.
You can find it's location from your maven setting.xml file , if not mentioned there then it will take the default path i.e. .m2 folder inside c drive.
You need to check first whether these jars are present there or not. If not, then either you need to copy those in proper folder structure or you can install those jars on your local firing below command :
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
The recommended way is:
use a Maven Repository manager such as Nexus
configure Maven to use a Single Repository Group
configure proxy repos to other Maven Repositories as needed and add them to the group you specified in your settings file (see previous step)
if not already available, configure a repo for 3rd party libs in your repository manager and add it to the group you specified in your settings file (see previous step)
if your dependencies are not available from any repo, then add them to your 3rd party repo
remove all JARs from the source code

Maven and m2e - build jar with dependencies information

We are using Maven and m2e tools for our development and today we encountered a problem.
One of our projects is small library that is required for other projects, so we packaged it into jar file and put in our private Maven repository.
For now, all of the jars that we put in this repository didn't have any external dependencies, but this library I mentioned uses some external jars.
Now, when I add information about this jar to other poms, this jar is downloaded from our private repository but Maven doesn't download dependencies needed by this jar.
I am wondering if I need to use some special target/add something to my pom.xml file that will inform Maven to include information about dependencies needed by this artifact.
EDIT:
Here is the workflow I perform when I upload jar to our private repository:
1.I generate jar file from Eclipse using m2e
2.mvn install:install-file -DgroupId=<your_group_name> -DartifactId=<your_artifact_name> -Dversion=<snapshot> -Dfile=<path_to_your_jar_file> -Dpackaging=jar -DgeneratePom=true -DcreateChecksum=true
3.I copy folder created in my local repository to remote repository
If your small library is a maven project as you state, there should be no reason to have eclipse build the jar and then use maven to install it and then manually copy to the remote repo. Instead you should use m2e to run the deploy goal:
mvn deploy
That will cause the jar to get built and then install it directly into your local maven repo then deploy it to the remote repo.
In eclipse this can be accomplished by right clicking your project, choosing Run As -> Maven Build... then in the run configuration window for Goals input type deploy then click Run. After this has been done once, you can just use Run As -> Maven Build to run the same config again.
I see you use -DgeneratePom=true during the installation of the jar file. What you need to do is create a pom.xml for your artifact. In the pom.xml, you can specify the dependencies that your jar file requires. When executing the install:install plugin goal, you use -DpomFile=pom.xml instead.
The best way to do this is to run mvn deploy
You have to setup the distribution repository to your private artifact manager (nexus or artifactory) in your settings.xml
see this for more details

Categories

Resources