Download Maven Repository files - java

I have a Maven repository that has jar files I want to use locally on other projects. What is the best way to download those jar files from the Maven repo.
I went to http://mvnrepository.com/ but the jar files do not work in my build. I would like to have is the Maven build jar files, but I am not able to save them from my project in Netbeans.
Is there a way to download all the files within Maven repository?

There's probably thousands of jar files in maven central, so I wouldn't try to download all of them.
Generally, if you want to use jars found in maven repositories, you may want to start a maven project yourself; configure your pom.xml to require those dependencies, and they'll be downloaded automatically.
Most java IDEs have maven support or a maven plugin.
-- EDIT --
Here's a really quick pom.xml example from maven's website:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
If you're using eclipse, you'll probably want to m2e plugin to handle most of this for you. Plus, it will link the javadoc to those jars as well. :)

Why don't you try using Maven instead? With Maven you're able to automatically manage your dependencies easily.
It's really simple. Please check this Maven in 5 minutes so you can begin with it successfully. Any doubts, please let me know.
-- EDIT --
As soon as you learn some main concepts, like dependencies, you can simple start your projects with the following command:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
After that, you can simply import to the most used IDEs like Eclipse, using:
mvn eclipse:eclipse

The tool mvn2get (https://github.com/groboclown/mvn2get) will download published Maven artifacts suitable for use in a local repository. This includes the POM files, checksum files, and signature files, as well as able to search through dependencies.
It allows for a one-line execution to download the files:
$ mvn2get.py -d my-local-repo-dir --resolve log4j:log4j:1.2.17
I wrote this script based on a similar need to download into a local repository.

The obvious answer is to suggest that you consider building your code using Maven. This will give you native support for the Maven Central repository.
But... I sense that you just want to download the files you need to a local directory?
In that case I'd suggest using the Apache ivy command-line.
Example
The files you want are listed in a ivy.xml file. For example:
<ivy-module version="2.0" xmlns:e="http://ant.apache.org/ivy/extra">
<info organisation="com.myspotontheweb" module="demo"/>
<dependencies>
<dependency org="commons-lang" name="commons-lang" rev="2.6" conf="default"/>
<dependency org="junit" name="junit" rev="4.10" conf="default"/>
</dependencies>
</ivy-module>
And ivy can populate a local "lib" directory as follows:
java -jar ivy.jar -retrieve "lib/[artifact].[ext]" -ivy ivy.xml
The advantage of this approach is that ivy can download the additional transitive dependencies of the modules you've specified:
$ find lib -type f
lib/commons-lang.jar
lib/junit.jar
lib/hamcrest-core.jar
Note:
hamcrest-core is a depedency of junit.

Related

How do I get maven to pick up my local jar in repositories?

I am writing a package (PACK-A) that is consumed by another (PACK-B). For local development I want to use a local jar that is produced from PACK-A and use it in PACK-B.
The workflow would be
cd PACK-A; mvn clean; mvn package
cd PACK-B; mvn clean; mvn package (but pulls in the local PACK-A jar file)
I thought the way to do it was through the <repositories /> tag in the pom.xml file so my POM.xml file looks like this (lots taken out for brevity):
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.team</groupId>
<artifactId>PACK-B</artifactId>
<version>1.0.0</version>
<dependencies>
<groupId>com.company.team</groupId>
<artifactId>service-name</artifactId>
<version>0.3.0-master+17</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>service-name</id>
<url>file:///Users/username/Projects/service-name/target</url>
</repository>
</repositories>
</project>
file:///Users/username/Projects/service-name/target is the directory that compiles (mvn package) the service.jar.
It isn't picking it up and I've confirmed it using a decompiler. How do I get maven to use my local jar in the service that I compiled? Or is my <url> directory written out incorrectly?
Also, I've confirmed, via the decompiler, that the service does compile with my changes. It just isn't picked up by my consumer.
For local development I want to use a local jar that is produced from PACK-A and use it in PACK-B.
To achieve this, you "only need":
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.team</groupId>
<artifactId>PACK-B</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.company.team</groupId>
<artifactId>PACK-A</artifactId>
<!-- same version, or which ever suits you -->
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
And to run mvn install (which additionally to package installs the artifact into the local repository).
See also: How to build dependent project when building a module in maven
and What does mvn install in maven exactly do
It is a "complex field" (multi-module...), but the thing You are trying to achieve is really simple/101.

Maven project is missing package when compiling from terminal

I'm currently trying to compile my first maven project from the command line but whenever I run this command:
C:\Users\zacha\git\INF2050\tp1\PELZ07039904\src\main\java>javac analyse.java
I get this back
analyse.java:7: error: package org.apache.commons.io does not exist
But the project runs perfectly fine when I run it directly from InteliJ
Here is my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>PELZ07039904</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
</project>
I checked and all my jar are in my .m2 file so I'm not sure why I'm getting this error.
javac doesn’t have any idea about maven and it’s dependencies. All it can do is just compile a class. Thats it.
Instead you need to use maven specific command.
Maven provides a command line tool.
To build a Maven project via the command line, run the mvn command from the command line. The command should be executed in the directory which contains the relevant pom file. You need to provide the mvn command with the life cycle phase or goal to execute.
The Maven tooling reads the pom file and resolves the dependencies of the project. Maven validates if required components are available in a local repository. The local repository is found in the .m2/repository folder of the users home directory.
For example the mvn clean install command triggers the jar packaging. This includes compiling the sources, executing the tests and packaging the compiled files in a JAR file. As last step the install phase installs the resulting artifact into the local repository, so it can be used as dependencies by other Maven builds.
Check full guide here

Import JAR files to spring using maven

I tried to import some JAR files to my maven spring project using maven install plugin.
I placed the JARs in a lib folder in my base directory (where the POM.XML file is) and installed them one by one manually by running mvn install.
My xml looks like:
EDIT:
<dependency>
<groupId>com.keydoxWeb</groupId>
<artifactId>keydox</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>myPath\codecs.jar</systemPath>
</dependency>
<!-- and so on.. -->
Still telling me this error:
"Should use a variable instead of a hard coded path"
To import jars to your local repo, you generally would not have to or want to edit a pom.xml file. Rather there are shell commands that you can use to import the jars to your local maven repo (typically located at ~/.m2). Said commands are described here -- looks like this:
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
Once you do this, you'll also have to bring the dependencies into your projects pom.xml as explicit dependencies. That will look like this:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.0</version>
</dependency>
...
</dependencies>
Hope it helps!
Usually you do not have to import jars manually - they are installed by maven into local repository. And eclipse needs to know where this maven repository is. You may regenerate eclipse project files via
mvn eclipse:eclipse
(or switch to IntelliJ IDEA which opens maven projects natively)

Maven doesn't put downloaded jars in eclipse's JRE System Library "folder"

I have created project using this command:
mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
After downloading libraries my project looks like this:
http://i43.tinypic.com/2iut1me.jpg
How can I "tell" maven to download jars in just one folder?
Maven will not download jars in a single folder, instead, it create a dir structure in a $HOME/.m2/repository for managing your dependencies, including versions.
If you want the jars to show in the Eclipse project, create a pom.xml file in your project with the correct dependencies, for example
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.myGroup</groupId>
<artifactId>myProject</artifactId>
<name>myProjectName</name>
<version>myProjectVersion</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>dependency1</groupId>
<artifactId>dependency1</artifactId>
<version>dependency1</version>
</dependency>
<dependency>
<groupId>dependency2</groupId>
<artifactId>dependency2</artifactId>
<version>dependency2</version>
</dependency>
<!-- .... and so on -->
</dependencies>
Also, configure your project in eclipse to be a Maven project and then you will see "Maven Dependencies"in your project.

In what directory to put java libraries for maven project?

I found that Maven implies specific directory layout. But I don't understand from here: http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
where java libraries needed to compile and run my code should be placed. I think they shouldn't be placed under 'src/main/resources' because resources is something like images or so. Also it doesn't look right to place them under 'src/main/java'. If I wouldn't use maven, I'd place libraries in project's root lib directory. But I don't think that for maven project it will be right. Please advise.
UPD: I solved the problem. The matter was that I set packages for my sources as src.main.myApp instead of main.myApp. This seems to upset maven.
Maven handles your project dependencies in a different way to a 'Standard' Java project.
You declare the libraries you depend on in your project's pom.xml:
e.g.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>your-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>your-project-web</name>
<dependencies>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.5</version>
</dependency>
</dependencies>
</project>
When you use a maven command to build the project, i.e. mvn install, it will download the dependencies for you and store them in your local repository.
In Maven, you do not keep libraries in your project. You specify dependencies on these libs, and they get populated into your local repository. At the time of build, if you are packaging the libs (say for a war file), they do get pulled into target//WEB-INF/lib. But in general, the whole idea is not to deal with these libraries or manage them, just to manage dependencies in your pom file, and forget the rest.

Categories