How maven works in Jar downloading? - java

I'm working first time with Maven project. Just created new Maven project in eclipse and in pom.xml I've added below configuration. Like Spring, log4j's jar version etc
<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.LearnJavaSpring</groupId>
<artifactId>TalendJavaSpring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<properties>
<spring.version>5.1.4.RELEASE</spring.version>
</properties>
</project>
The moment I save my pom.xml file, It automatically creates folder at C:\Users\trisha\.m2 location with respective jars in it, the one I've mentioned in pom.xml.
1) My doubt is, From where maven got these jars automatically in above folder location ? Does maven downloads the dependencies automatically ?
2) Is it correct to think in this way that, Maven parse the pom.xml file when we save that and download all the dependencies or jars mentioned in pom.xml file ?
3) What if I want maven to download jars from the different location from where Maven does ? Does maven automatically downloads the latest jars ?
4) Is that possible to change this default folder location C:\Users\trisha\.m2 to other location ? If yes, How..?

1) My doubt is, From where maven got these jars automatically in above
folder location ? Does maven downloads the dependencies automatically
?
In pom.xml you mention something like this:
<groupId>xx</groupId>
<artifactId>yyy</artifactId>
<version>1.1</version>
It will download the xx version 1.1 library automatically.
Search xx in Maven local repository.
Search xx in Maven central repository.
Search xx in Maven remote repository (if defined in pom.xml).
2) Is it correct to think in this way that, Maven parse the pom.xml
file when we save that and download all the dependencies or jars
mentioned in pom.xml file ?
Yes, exactly.
Reference
3) What if I want maven to download jars from the different location
from where Maven does ? Does maven automatically downloads the latest
jars ?
yes , see the above 2 answers combined. You can also ad jars manually by building path and then selecting the required jars. Better suggestion is always to use maven.
To get the latest Jar there are some parenthesis tweaks which you can find here
4) Is that possible to change this default folder location
C:\Users\trisha.m2 to other location ? If yes, How..?
yes, it is possible.
In your settings.xml change the below lines:<localRepository>C:\Users\me\.m2\repo</localRepository> to point to your desired folder.

Related

No files were downloaded for org.springframework:org.springframework.core:3.2.2.RELEASE

I created a small java application using intellij, later I updated that project as Maven project using "Add framework support" option. When I tired to add spring jar file on project I got following error saying,"No files were downloaded for org.springframework:org.springframework.core:3.2.2.RELEASE".
Following are the steps I did to add spring jar files
1)Clicked on File -> Project Structure -> Modules -> Clicked on "+" Sign -> Library -> From Maven
2)Searched for org.springframework.core and selected 3.2.2 Release
3)Downloaded to lib folder under my project.
4)Got Error.
I don't know where it went wrong. I am new to java and spring application.
As already mentioned, Maven downloads dependencies itself (a reason why it's a build automation tool).
To add Spring as a dependency, add this code to Maven's pom.xml (the file is in your module's root directory):
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
</dependencies>
Also, delete the lib directory from your project root - Maven doesn't put jars there.
Maven downloads the dependencies automatically, can you try adding this to the pom.xml file. Maven should automatically download the dependencies..
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>

Specific spring-core jar to be included in project

Ive included following lines in my pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
The specific spring-core JAR version I want is 4.1.6, but whenever I do an mvn clean install, the dependencies folder under my project in netbeans shows a downgraded version (currently 3.0.7.RELEASE).
I want to know how I can force maven to put 4.1.6.RELEASE jar in my dependencies folder. I've done
mvn dependency:purge-local-repository -DactTransitively=false -DreResolve=false
And I've the folder 4.1.6.RELEASE with correct jar in my local /.m2/repositories folder. I need to access org.springframework.util.MimeType class which is not available in spring-core 3.0.7.RELEASE jar.
See the result of mvn dependency:tree to know if another jar bring back the version 3.0.7. and exclude from this dependency the spring-core
See the same result to know if the right version is present
Add the pom.xml and the result of mvn dependency:tree in the question
Please follow these steps:
Check in .m2 local repository where the Spring 3.0.7 jar is downloaded(It should be under folder org->springframework)
Delete the folder present inside springframework, so that mvn will try to fetch required dependencies.
Let me know if it helps
The issue is that spring-core is a mandatory dependenecy of most spring artifacts. Most likely you are using a dependent artifact e.g spring-context which is of version 3.0.7. Then Maven is fetching spring-core of the same version as the dependent artifact using its transitive dependency mechanism.
To control the version of spring-core you need to move its declaration from the dependencies section to the dependency management section of your pom.xml as follows
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6</version>
</dependency>
</dependencies>
</dependencyManagement>

Using Git with IntelliJ and Maven

If I wanted to use just the normal git via the command line and not the one in IntelliJ, what do I need to include in the version control so when I download it, I can get the Maven libraries without manually installing them?
Edit: There is no pom.xml file when the libraries are added to an IntelliJ project, so I was wondering what I need to include so Maven inside IntelliJ can download the libraries.
what do I need to include in the version control so when I download it, I can get the Maven libraries without manually installing them?
The pom.xml file does this:
Some of the configuration that can be specified in the POM are the project dependencies, the plugins or goals that can be executed, the build profiles, and so on. Other information such as the project version, description, developers, mailing lists and such can also be specified.
Running mvn install will cause Maven to download your dependencies.
Intellij will automatically understand the changes in the pom files and update libraries of course you should have pom.xml file.
If its a maven based project, you definitely need a pom.xml file like below
<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.test</groupId>
<artifactId>testing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testing</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Once you have a pom.xml file, you can define dependencies like the JUnit dependency defined above with the version you need and maven will automatically take care of downloading the dependency for the project. Once you add any new dependency to the pom.xml, you can run "mvn clean install" from the directory where you have the pom.xml file so that it installs the new dependency.
Hope this helps.

No versions available for org.eclipse.swt.win32.win32:x86_64:jar

While trying to depend on piccolo2d-swt-examples artifact (in m2e), I am getting the following message
VersionRangeResolutionException: No versions available for org.eclipse.swt.win32.win32:x86_64:jar:[3.3.0-v3346,) within
specified range
What is the meaning of this message? Does it mean that there is no library for win32 at all? Or it means that SWT is not under maven control?
UPDATE
My current POM is below.
Currently it has no any explicit dependency on SWT. Being not fluent with Maven, I can't judge if this message means, that Maven feels my SWT version from global settings and reports, that no library written for it, or it just can't find any required SWT libraries in repository.
In first case I can't use SWT version of Piccolo at all (it is not portable, since not written for all platforms) while in second case I can use it, but need to pack SWT for Maven in local repository.
This is the question.
<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>tests</groupId>
<artifactId>Piccolo2D_3_Tests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.piccolo2d</groupId>
<artifactId>piccolo2d-swt-examples</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
</project>
VersionRangeResolutionException: No versions available for
org.eclipse.swt.win32.win32:x86_64:jar:[3.3.0-v3346,) within specified range
According to a maven central query there is no x86_64 artifact for that swt jar file but there is an x86 artifact.
You might want to try forcing your piccolo2d dependency to activate its windows_x86 profile explicity using mvn -Pwindows_x86 or (UPDATE 2) try building with a 32-bit Java JDK.
UPDATE 1: Your problems look similar to piccolo Issue 203: Missing maven profile for Windows x86_64.
I downloaded the the missing artifact from here(http://www.java2s.com/Code/Jar/o/Downloadorgeclipseswtwin32win32x866442jar.htm) and installed the jar using
>mvn install:install-file -Dfile=org.eclipse.swt.win32.win32.x86_64-4.2.jar -DgroupId=org.eclipse.swt.win32.win32 -DartifactId=x86_64 -Dversion=4.2 -Dpackaging=jar
And it started working fine :)
PS: This way you don't need to tamper with the profiles at all.
This is from the Piccolo readme:
To include the Piccolo2D core classes in your project, use a
dependency of
<dependency>
<groupId>org.piccolo2d</groupId>
<artifactId>piccolo2d-core</artifactId>
<version>1.3.1</version>
</dependency>
in your pom.xml. To include the Piccolo2D core classes and the
Piccolo2D extras classes in your project, use a dependency of
<dependency>
<groupId>org.piccolo2d</groupId>
<artifactId>piccolo2d-extras</artifactId>
<version>1.3.1</version>
</dependency>
in your pom.xml. To include the Piccolo2D core classes and the
Piccolo2D SWT classes in your project, use a dependency of
<dependency>
<groupId>org.piccolo2d</groupId>
<artifactId>piccolo2d-swt</artifactId>
<version>1.3.1</version>
</dependency>
Does adding these dependencies change anything?

Maven POM configuration & Tomcat shared/lib dependencies

I'm new to Maven and I created a new web app to "migrate" an old application and to start using Maven 3. This application uses some libraries (jars) and most of them are under the shared/lib folder in Tomcat (5.5) directory.
How can add these libs to Maven POM? Should I add them at all?
I forgot to mention that some of these jars cannot be found in Maven repository since are more like utility libraries that are common to most of the projects.
In the <dependencies/> section of the POM, you can declare the shared jar as a "system" scoped dependency. This scope recognizes a <systemPath/> child element, allowing you to point to the filesystem location on the local (build) filesystem.
See Maven "System" dependencies
example:
<project>
…
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>someDependency</artifactId>
<version>1.0.1</version>
<scope>system</scope>
<systemPath>${tomcatHome}/shared/lib/someDependency-1.0.1.jar</systemPath>
</dependency>
</dependencies>
…
</project>
Use
<dependency>
tags to specify the libraries. And if some of the libraries are not found in the maven repository. Specify the repository using
<configuration>

Categories

Resources