Use maven to "extend" existing jar file - maven

My project contains a couple of class which have to be integrated into an final jar file.
So my current "workflow" looks like:
mvn compile && jar uf contribution.jar -C target/classes .
I guess this could be done with the maven-jar plugin or through maven-assemblies but can't figure out how.

Simple - add the element <packaging>jar</packaging> to your pom.xml and invoke the following:
mvn package
See Maven build lifecycle for more information. It really is a must-read if you're planning to use Maven for you project.
Edit
Thanks for the update, I know understand what you mean. There may be a better way to do this, but this is what I use:
Maven will automatically include this jar if you add it as a dependency in the usual way, i.e:
<dependencies>
...
<dependency>
<groupId>some.group</groupId>
<artifactId>contribution</artifactId>
<version>1.0.0</version>
</dependency>
...
</dependencies>
The question is how to ensure that maven can find the jar? If the jar were in the Maven Global repository it would find it automatically, so always check this first. If not, then you will have to create your own repository to store custom jars. This may not be the most clever way to do it, but it's how I do it. Note that this is not the same thing as the cache stored in the .m2 folder.
Let's say we want to use c:\maven\repo as our local. To do this, first ensure the folder exists then add the following lines to your pom.xml:
<repositories>
<repository>
<id>my-repo</id>
<url>file://c:\maven\repo</url>
</repository>
</repositories>
Next, you will need to manually add the contribution jar to this repo. To do this execute the mvn deploy:deploy-file command and give it the appropriate values. See here for more information.
Now when build/compile/package your original project, it should include contribution.jar in the packaging as it is a dependency.
Hope this helps :)

Related

In a Springboot Maven project, how to reference from another Jar?

I have a SpringBoot Maven project. I am dependent on another set of libraries. Currently am pointing to their repository path , downloading it to .m2 repository and using.
But the repository website is not reliable. SO I wanted to package the dependent libraries as part of JAR in the resources folder.
After putting the jars in resource folder. How can I get references of the Types/libraries ?
Currently:
<dependencies>
<dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-core</artifactId>
<version>5.23.1</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>www.dcm4che.org</id>
<name>dcm4che Repository</name>
<url>https://www.dcm4che.org/maven2</url>
</repository>
</repositories>
You can put all your libraries in a single folder either in your project or in some folder in your local. You can then add them to your maven POM.
Lets say you put all your jars in a folder called libs in your base project directory. You can then add something similar to the below in your maven POM.
<groupId>com.abc.xyz</groupId>
<artifactId>my-artifact-id</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/libs/my-jar-name.jar</systemPath>
You need to use the scope system. Excerpt from Maven website,
Scope : system - This scope is similar to provided except that you
have to provide the JAR which contains it explicitly. The artifact is
always available and is not looked up in a repository.
Maven Documentation
If you read further, it also mentions that this has been deprecated. So, its a nice quickfix or a hack but then the best thing would be to set up a repository manager as suggested by others.

Can not build standart example "NetBeans Platform Quick Start Using Maven"

I tried to perform muti-module example for integration maven and netbeans (https://platform.netbeans.org/tutorials/nbm-maven-quickstart.html) step by step. When I finished coding I have known that it's necessary to add libs org.openide.util and org.openide.util.lookup in my maven app. So I can't find the way to do this.
I find some repos which contains this libs:
oss.sonatype.org/content/repositories/releases/com/googlecode/sarasvati/thirdparty/netbeans/org-openide-util/7.3/ and
repo1.maven.org/maven2/com/googlecode/sarasvati/thirdparty/netbeans/ )
But when I trying to add this repo in netbeans it refuse to index it.
Then I'm trying to add repo in pom.xml. In TextFilter module I'm add:
... <modelVersion>4.0.0</modelVersion>
<repositories>
<repository>
<id>org_openide_util_repo</id>
<url>https://repository.sonatype.org/service/local/repositories/atlassian/content/org/codeartisans/thirdparties/swing/org-openide-util/8.6.2/</url>
</repository>
</repositories>
...
<dependency>
<groupId>org.codeartisans.thirdparties.swing</groupId>
<artifactId>org-openide-util</artifactId>
<version>8.6.2</version>
</dependency>
<dependency>
<groupId>org.codeartisans.thirdparties.swing</groupId>
<artifactId>org-openide-util-lookup</artifactId>
<version>8.3.1</version>
</dependency> ...
But when I trying "build with dependencies" MavenPlatformWordApp, I get mistake:
Failed to execute goal
org.codehaus.mojo:nbm-maven-plugin:3.13:cluster-app
(default-cluster-app) on project MavenPlatformWordApp-app: Failed to
retrieve the nbm file from repository: Could not find artifact
org.codeartisans.thirdparties.swing:org-openide-util:nbm:8.6.2 in
netbeans (bits.netbeans.org/nexus/content/groups/netbeans/)
I'm tryed to add reference to repo in pom.xml MavenPlatformWordApp, them mistake takes the form:
Failed to execute goal
org.codehaus.mojo:nbm-maven-plugin:3.13:cluster-app
(default-cluster-app) on project MavenPlatformWordApp-app: Failed to
retrieve the nbm file from repository: Could not find artifact
org.codeartisans.thirdparties.swing:org-openide-util:nbm:8.6.2 in
org_openide_util_repo
(repository.sonatype.org/service/local/repositories/atlassian/content/org/codeartisans/thirdparties/swing/org-openide-util/8.6.2/)
Google search is nothing to help me. I can't understand whats the problem, please try me.
And one more thing:
If follow example instructions, filds "TextFilter filter" and "Lookup.getDefault()" in TextTopComponent.java in MavenWordEngine module are not existing. I'm add import org.openide.util.Lookup on top of TextTopComponent.java and trying to add import com.mycompany.textfilter.TextFilter. But TextFilter is not visible in TextTopComponent.java despite the fact that I maked the TextFilter Interface public. Will you explain my mistakes?
Replace the url-Element in your POM with this:
<url>https://repository.sonatype.org/service/local/repositories/atlassian/content</url>
Then the dependencies to org-openide-util and org-openide-util-lookup can be resolved.
The URL that you are using points to a single artifact and not to the whole repository.
Update
Maven says that it is looking for org.codeartisans.thirdparties.swing:org-openide-util:nbm:8.6.2 I don't know why. The artifcat type nbm belongs to netbeans, and I'm not familiar with netbeans. Try this:
<dependency>
<groupId>org.codeartisans.thirdparties.swing</groupId>
<artifactId>org-openide-util-lookup</artifactId>
<version>8.3.1</version>
<type>jar</type>
</dependency>

Adding db2 jars to java webapp using maven

I'm trying to add the following db2 jars to my Java web application using Maven...
db2jcc_license_cu.jar
db2jcc_javax.jar
db2jcc.jar
I'm following the instructions posted in this post...
Can I add jars to maven 2 build classpath without installing them?
I want to use the static in-project repository solution. So far I have...
Created a folder in my root directory named lib. Inside this
directory lives the three db2 jars.
Added the following to my pom file...
<repository>
<id>lib</id>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>file://${project.basedir}/lib</url>
</repository>
</repositories>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc</artifactId>
<version>3.8.47</version>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc_license_cu</artifactId>
<version>3.8.47</version>
</dependency>
But when I run a maven install I get ...
[WARNING] The POM for com.ibm.db2.jcc:db2jcc:jar:3.8.47 is missing, no dependency information available
[WARNING] The POM for com.ibm.db2.jcc:db2jcc_license_cu:jar:3.8.47 is missing, no dependency information available
I got the version of the Jars by running a...
java com.ibm.db2.jcc.DB2Jcc -version
Have I specified this version info corretly? Can anyone suggest what I am doing wrong?
The problem is that you didn't install the jars properly in your "project-maven-repository" (i.e. in the folder ${project.basedir}/lib)
Maven stores (when you do mvn install) the jar files in a maven repository. A maven repository have precise hierarchical structure. Here is a simplified vision of this structure:
the artifact groupId+artifactId define the first part of folder path (in the repository) where the artifact is stored.
the artifact version is the second part of the folder path
the artifact version is also a suffix to the artifact name
the artifactId is the artifact name
the packaging is the artifact extension (default is jar)
By default maven use a repository located under <USER_HOME>/.m2/repository
The solution you are trying to setup use another location for the repository : ${project.basedir}/lib and even if it is not the default repository location it is still a maven-repository and so maven is expecting to find the usual maven repository hierarchy under this location.
That's why you need to organize your ${project.basedir}/lib folder just like a maven repository. That's explained in this part of the referenced post:
Use Maven to install to project repo
Instead of creating this structure by hand I recommend to use a Maven plugin to install your jars as artifacts. So, to install an artifact to an in-project repository under repo folder execute:
mvn install:install-file -DlocalRepositoryPath=lib -DcreateChecksum=true -Dpackaging=jar -Dfile=[your-jar] -DgroupId=[...] -DartifactId=[...] -Dversion=[...]
If you'll choose this approach you'll be able to simplify the repository declaration in pom to:
<repository>
<id>repo</id>
<url>file://${project.basedir}/lib</url>
</repository>
So you need to do an mvn install to create the ${project.basedir}/lib hierarchy (you can do it by hand, but it's not recommended and error prone).
I your case, the commands to run will be like this: (assuming you put the jar in your HOME_DIR and run this command in your ${project.basedir})
mvn install:install-file -DlocalRepositoryPath=lib -DcreateChecksum=true -Dpackaging=jar -Dfile=<USER_HOME>/db2jcc_license_cu.jar -DgroupId=com.ibm.db2.jcc -DartifactId=db2jcc_license_cu -Dversion=3.8.47
What are the advantages of the approch you choose :
a developer with no maven setup will have the libraries available inside the project sources, under SCM system.
you can easily reference jars that aren't in a public maven repository without the need of something like artifactory or nexus
The drawbacks :
a quite complex folder structure under ${project.basedir}/lib looking very strange for someone not used to work with maven.
you will store the libraries under SCM (lot's of huge binary files)
Another solution would be to download those jars before hand and put them somewhere relatively to your project (like lib directory). Now just tell maven to use those jars. Here the groupId, artifactdId and version are JFYI since they won't be used to download anything.
The merit of this solution is that you won't have to build a maven repository.
<dependencies>
...
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>licences</artifactId>
<version>1.0</version> <!-- Adjust this properly -->
<scope>system</scope>
<systemPath>${basedir}/lib/db2jcc_license_cu.jar</systemPath>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc4</artifactId>
<version>1.0</version> <!-- Adjust this properly -->
<scope>system</scope>
<systemPath>${basedir}/lib/db2jcc4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc_javax</artifactId>
<version>1.0</version> <!-- Adjust this properly -->
<scope>system</scope>
<systemPath>${basedir}/lib/db2jcc_javax.jar</systemPath>
</dependency>
</dependencies>
Refer Link (Japanese): Mavenリポジトリで提供されていないサードパーティJarをどうするか
I guess these jars do not have a pom.xml. Hence the warning. If the jars get packaged and the application works, then I guess you do not have a problem.

Maven/Spring: How to add external jar to classpath without installing it as maven dependency?

The common ways of including external non-maven jar in the classpath of your Maven project is to either use "addjar-maven-plugin" (which I have not been able to get to compile maven with) or "mvn install:install-file" and then adding a dependency section for the external JAR. This approach essentially installs client JAR in your repo and makes it available in classpath for maven. But is there a better way to do this (or) are the options mentioned above the only ones available? I just want the external JAR to be added to classpath for component scanning by Spring but do not want the JAR itself to be added to my repo as it is client's JAR? I hope this is a valid case (if not, kindly explain)
Thanks,
Paddy
You can create lib folder under your project's src folder and reference this folder as maven repository.
<repository>
<id>local</id>
<url>file://${basedir}/src/lib</url>
</repository>
Then you should add dependency to your jar in your pom.xml
<dependency>
<groupId>com.company</groupId>
<artifactId>dependency</artifactId>
<version>1.0</version>
</dependency>
After that your should rename jar file and place it on following path src/lib/com/company/dependency/1.0/dependency-1.0.jar . This path depends on how you want to reference your jar.

about generate maven dependency

I am pretty new to maven.
Now I have a maven project developed. My another project needs to depend on this one.
Does anyone know how can I generate my own dependency? So that my second project can add the first one as a dependency in pom.
thank you very much
Since your first project is already a maven-project, just install it in your local repository by running mvn install in the first project's root directory.
Then you can include a dependency in your second project by simply referencing the groupId, artifactId and version you defined in the first project.
So if your first project had the following in its pom:
<project>
<groupId>com.yourdomain</groupId>
<artifactId>yourcomponent</artifactId>
<version>1.0</version>
... <!-- more here -->
you can include this in your second project:
<dependencies>
<dependency>
<groupId>com.yourdomain</groupId>
<artifactId>yourcomponent</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
Unless you deploy your project 1 jar to a central maven repository, this will only work if your jar is in your local repository (via mvn install).
Maven projects are identified by the "Maven coordinates", that is, the ArtifactID, GroupID and version.
Say you create your first project and run maven install. Your local repository (in $HOME/.m2/) will now contain the compiled project plus whatever coordinates you put in there.
Your second project must now only depend on the said coordinates.
I would suggest googling a bit on maven. I made a tutorial a long time ago that might help you, even if the examples are a little simple. Here you go and good luck!

Resources