a quick way to maven install a lib folder - java

is there a quicker way to maven install say a complete lib folder to maven dependencies rather than the individual command for each jar:
mvn install:install-file -DgroupId=third-party -DartifactId=app-eventinfo -Dversion=1.0 -Dpackaging=jar -Dfile=EventInfoToOrder.jar
Is there a way I can say this is a folder of dependencies or a little work around in eclipse where I trick maven into using a user defined library as a dependency.
Yes I have a lot of folders that have been sitting on my buildpath which I must install to my local repo.
Cheers for reading :)

The problem is not about the folder contents, but specifying the correct values for groupId, artifactId and version.
If you are ok with random values for this, then you could write a dirty batch file/shell script to do this.
Otherwise, the time you spend trying to automate this (by having a mapping of groupId, artifactId and version for each jar in your folders), you can arguably do mvn install:install-file manually.
A better option would be to review your dependencies and see which are not available in maven repo and only install those. The remaining will be downloaded by maven on its own.

There is no standard way of doing this as it would go against Maven's philosophy of managed (versioned) dependencies.

Related

How to find the dependencies of a maven plugin goal

I'm trying to find out where the dependencies are coming from for a particular plugin goal. I know the dependencies exist because Maven downloaded them when I ran the goal for the first time. And they're not (directly) dependencies of my project, because I ran mvn clean install first, and these dependencies weren't downloaded then.
In this specific case, I'm trying to figure out what the dependencies are when I run mvn sonar:sonar, but I expect the answer will be general purpose. For instance, even though I've built this project a number of times, when I ran that goal Maven downloaded a bunch of new jars like maven-antrun-plugin.
Here are things I've tried:
mvn dependency:tree shows the dependencies for the project, but not for the plugin goal (it doesn't include anything related to SonarQube in the list).
mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:effective-pom -Dverbose=true also doesn't include anything related to SonarQube.
mvn -X sonar:sonar prints out what looks like a dependency graph, but it's missing the jars that Maven downloaded the first time I ran the sonar:sonar goal.
mvn -X dependency:resolve-plugins seems to be meant to download the dependencies of plugins, but does not capture the sonar:sonar dependencies. If I clear out my Maven cache, run mvn dependency:resolve-plugins, and then run mvn sonar:sonar, Maven has to download jars.
Use your IDE to navigate to the POM of the plugin project and then look at the dependency tree.
Or do this manually by copying the plugin POM to your file system and running mvn dependency:tree.
The dependencies downloaded by maven are generally stored locally on the pc at the address C:\Users\yourUser/.m2 this regardless of your projects and so that you do not have to reload dependencies that you are already using in other projects.
I hope I have understood your question and that my answer is useful to you, greetings.

Multiple local jars needed for my maven project but should work for others too

I have a Java maven project that depends on 50 jars that is not in Central Maven Repo or in my organization's Nexus repository. I don't want to do mvn install:install-file ... as it will work for me but it won't work for others unless they also run this command and install all the jars in their local repo.
I can put all my jars in project lib folder and check it in. But I need to hard code the path in the pom.xml for these jars.
Is there a way to handle this?

POM files for Websphere specific jars

Websphere offers a set of provided jars, including com.ibm.ws.ejb.thinclient_8.5.0.jar, com.ibm.ws.batch.runtime.jar, com.ibm.ws.orb_8.5.0.jar, etc.
In the ANT build process, some people had these files on the classpath. Now we are moving to Maven, and I am not sure what I should do with these files:
If they should be part of the build process, I need to put them into the repository. But how should I get or generate proper POMs for them?
If they should not be part of the build process, what are proper replacements?
If you are using a company maven repository as a proxy to maven central, the best thing to do is to make these jar files available there:
mvn install:install-file -Dfile=<path to the jarfile> -DgeneratePom=true -Dpackaging=jar -Dversion=<version> -DgroupId=<groupId> -DartifactId=<artifactId>
In such a case the groupId is usualy composed by your company prefix and then the base package of the artifact. The artefactId would be the last part without the version. For example for com.ibm.ws.ejb.thinclient_8.5.0.jar, the version is 8.5.0, the artifactId thinclient and the groupId something like com.example.thirdparty.com.ibm.ws.ejb.
The same approach works as well if you are the sole developer and install these artifacts in your local repository.
See also the official documentation
Another approach would be to have these files as part of the project and reference them through a local path and install it from there either using the maven-install-plugin or by issuing the steps from the first approach as part of the build process. See Maven and adding JARs to system scope and Maven: add a dependency to a jar by relative path.
Disclaimer: I always used the first option, as this seems to be the proper way.
Try the "was_public" JAR and POM shipped along with WebSphere Application Server traditional, starting with Version 8.
See here.

Download a Maven artifact and install into a specific local repository (not .m2)

I have several local Maven repositories besides the one located in ~/.m2 directory and I want to simplify the process of installing new artifacts into them from Maven central.
So far I couldn't find a way to tell mvn dependency:get that dependency should be put into a specific local repository.
I did manage to find a way to install a given downloaded artifact using mvn install:install-file -DlocalRepositoryPath=, but I want to be able to get and put dependencies into a specific repository with as few manual steps as possible.
Before you ask why can't I just configure my project to use Maven central directly here is the answer: the project uses Gradle and I do not own its build script (i.e. I can't modify it). The project build script is written to work with several distinct repositories having the same base URI that I fortunately can change using build.properties file. So my idea is to have several local maven repositories in the same root directory and trick the build script to use them.
You can use the maven.repo.local property:
mvn dependency:get -Dmaven.repo.local=/path/to/localrepo

Maven: How to include jars, which are not available in reps into a J2EE project?

in my J2EE project I've a couple of dependencies, which are not available in any Maven repository, because they're proprietary libraries. These libraries need to be available at runtime, so that have to be copied to target/.../WEB-INF/lib ...
Right now, I'm listing them as system dependency in my POM, but with this method the problem is, that aren't being copied to the target build during compilation. Also this method is not very elegant.
So which is the best way to integrate them in Maven?
Note: I don't want to create my own Maven repository.
For people wanting a quick solution to this problem:
<dependency>
<groupId>LIB_NAME</groupId>
<artifactId>LIB_NAME</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/WebContent/WEB-INF/lib/YOUR_LIB.jar</systemPath>
</dependency>
just give your library a unique groupID and artifact name and point to where it is in the file system. You are good to go.
Of course this is a dirty quick fix that will ONLY work on your machine and if you don't change the path to the libs. But some times, that's all you want, to run and do a few tests.
EDIT: just re-red the question and realised the user was already using my solution as a temporary fix. I'll leave my answer as a quick help for others that come to this question. If anyone disagrees with this please leave me a comment. :)
As you've said you don't want to set up your own repository, perhaps this will help.
You can use the install-file goal of the maven-install-plugin to install a file to the local repository. If you create a script with a Maven invocation for each file and keep it alongside the jars, you (and anyone else with access) can easily install the jars (and associated pom files) to their local repository.
For example:
mvn install:install-file -Dfile=/usr/jars/foo.jar -DpomFile=/usr/jars/foo.pom
mvn install:install-file -Dfile=/usr/jars/bar.jar -DpomFile=/usr/jars/bar.pom
or just
mvn install:install-file -Dfile=ojdbc14.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0 -Dpackaging=jar
You can then reference the dependencies as normal in your project.
However your best bet is still to set up an internal remote repository and I'd recommend using Nexus myself. It can run on your development box if needed, and the overhead is minimal.
Create a repository folder under your project. Let's take
${project.basedir}/src/main/resources/repo
Then, install your custom jar to this repo:
mvn install:install-file -Dfile=[FILE_PATH] \
-DgroupId=[GROUP] -DartifactId=[ARTIFACT] -Dversion=[VERS] \
-Dpackaging=jar -DlocalRepositoryPath=[REPO_DIR]
Lastly, add the following repo and dependency definitions to the projects pom.xml:
<repositories>
<repository>
<id>project-repo</id>
<url>file://${project.basedir}/src/main/resources/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>[GROUP]</groupId>
<artifactId>[ARTIFACT]</artifactId>
<version>[VERS]</version>
</dependency>
</dependencies>
You need to set up a local repository that will host such libraries. There are a number of projects that do exactly that. For example Artifactory.
None of the solutions work if you are using Jenkins build!! When pom is run inside Jenkins build server.. these solutions will fail, as Jenkins run pom will try to download these files from enterprise repository.
Copy jars under src/main/resources/lib (create lib folder). These will be part of your project and go all the way to deployment server. In deployment server, make sure your startup scripts contain src/main/resources/lib/* in classpath. Viola.
you can install them in a private, local repository (e.g. .m2/repository under your home directory): more details here
If I am understanding well, if what you want to do is to export dependencies during the compilation phase so there will be no need to retrieve manually each needed libraries, you can use the mojo copy-dependencies.
Hope it can be useful in your case (examples)
#Ric Jafe's solution is what worked for me.
This is exactly what I was looking for. A way to push it through for research test code. Nothing fancy. Yeah I know that that's what they all say :) The various maven plugin solutions seem to be overkill for my purposes. I have some jars that were given to me as 3rd party libs with a pom file. I want it to compile/run quickly. This solution which I trivially adapted to python worked wonders for me. Cut and pasted into my pom. Python/Perl code for this task is in this Q&A: Can I add jars to maven 2 build classpath without installing them?
def AddJars(jarList):
s1 = ''
for elem in jarList:
s1+= """
<dependency>
<groupId>local.dummy</groupId>
<artifactId>%s</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/manual_jars/%s</systemPath>
</dependency>\n"""%(elem, elem)
return s1
Continue to use them as a system dependency and copy them over to target/.../WEB-INF/lib ... using the Maven dependency plugin:
http://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html
Install alone didn't work for me.
mvn deploy:deploy-file -Durl=file:///home/me/project/lib/ \
-Dfile=target/jzmq-2.1.3-SNAPSHOT.jar -DgroupId=org.zeromq \
-DartifactId=zeromq -Dpackaging=jar -Dversion=2.1.3

Categories

Resources