8

I'm just transitioning from .NET to JAVA and have to start JAVA project with Spring 3.0.0. and Hibernate.

Can someone please explain to me step_by_step how to download spring dependencies with Maven. I just spent whole day with absolutely no success.

PS. I have Java 1.5.06 and have already downloaded spring (with no dependencies) and installed Maven.

edit:

c0mrade:

I think as of spring 3.0.0. they are considered to be optional dependencies so they need to be included separately each dependency, this is just a guess I'm not sure about this, if Pascal or someone more experienced confirms my statement then its true, so far I've worked with spring 2.5.5

Yes... They are definitely optional so this is what I did. I simply copy/pasted hibernate dependencies from spring-orm pom file to myproject pom file, meaning that now I have spring and hibernate dependencies in my pom file defined together. Then I ran "mvn install" on myproject and after that just hand copied all spring and hibernate jars to my project's lib folder.

So now I have a Java project with spring and hibernate. :)

I'm learning Java and this is just my second day so so please tell me if I did something horribly wrong.

update:

rlovtang:

When using maven you don't manually download any dependencies (like spring), neither do you hand copy any jars to your projects lib folder. All this is taken care of automatically by maven when you run 'mvn install'. How do you package your application, is it war?

I understand that. And it's clear to me that Maven automatically manages classpath for dependencies in my local repository so my project can work normally on my local machine. And I also red that you have an option to pack your dependencies jars in your WAR or EAR, but what if I want to pack my application as JAR together with all dependencies JARs inside output (target) folder? You see, I don't want to deploy my JAR file with pom.xml only, but all the JARs that are needed for my application to run.


It still does not work:

this is my pom.xml:

<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>

<dependencies>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>3.0.0.</version>
    </dependency>
</dependencies>

When I hit "mvn install" I get this exception:

[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [ERROR] FATAL ERROR [INFO] ------------------------------------------------------------------------ [INFO] null [INFO] ------------------------------------------------------------------------ [INFO] Trace java.lang.NullPointerException at org.apache.maven.artifact.versioning.DefaultArtifactVersion.parseVersion(DefaultArtifactVersion.jav a:136) at org.apache.maven.artifact.versioning.DefaultArtifactVersion.(DefaultArtifactVersion.java:47) at org.apache.maven.artifact.versioning.VersionRange.createFromVersion(VersionRange.java:219) at org.apache.maven.project.artifact.ProjectArtifactFactory.create(ProjectArtifactFactory.java:37) at org.apache.maven.project.DefaultMavenProjectBuilder.processProjectLogic(DefaultMavenProjectBuilder. java:1017) at org.apache.maven.project.DefaultMavenProjectBuilder.buildInternal(DefaultMavenProjectBuilder.java:8 80) at org.apache.maven.project.DefaultMavenProjectBuilder.buildFromSourceFileInternal(DefaultMavenProject Builder.java:508) at org.apache.maven.project.DefaultMavenProjectBuilder.build(DefaultMavenProjectBuilder.java:200) at org.apache.maven.DefaultMaven.getProject(DefaultMaven.java:604) at org.apache.maven.DefaultMaven.collectProjects(DefaultMaven.java:487) at org.apache.maven.DefaultMaven.getProjects(DefaultMaven.java:391) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:272) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138) at org.apache.maven.cli.MavenCli.main(MavenCli.java:362) at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315) at org.codehaus.classworlds.Launcher.launch(Launcher.java:255) at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430) at org.codehaus.classworlds.Launcher.main(Launcher.java:375) [INFO] ------------------------------------------------------------------------ [INFO] Total time: < 1 second [INFO] Finished at: Thu Feb 18 11:19:44 CET 2010 [INFO] Final Memory: 1M/2M [INFO] ------------------------------------------------------------------------

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Goran
  • 1,807
  • 7
  • 27
  • 41
  • 1
    I'd also recommend that you read free ebook maven by example , I basically learned maven in few days, of course the basics .. but I still ask every now and then question or two about maven – ant Feb 18 '10 at 10:15
  • 1
    @Goran pokusaj 3.0.0.RELEASE as Kees de Kooter suggested – ant Feb 18 '10 at 10:27
  • Still the same... I tried "3.0.0.RELEASE", and also tried "spring-context" for the artifactId but it still gives me this same error. – Goran Feb 18 '10 at 10:28
  • Near duplicate here: http://stackoverflow.com/questions/2237537/which-maven-dependencies-to-include-for-spring-3-0/2237805 – Tim Feb 18 '10 at 10:46
  • 1
    @Tim maybe you ought to help him instead of looking for duplicate questions – ant Feb 18 '10 at 11:24
  • 1
    @Goran, how are things going are you done yet ? – ant Feb 18 '10 at 13:09
  • Still reading the book, but to cut things short... I went into the org.springframework.orm folder of my spring directory and found pom.xml with all spring-orm dependencies in there. Launched mvn install and it downloaded hibernate for me (along with other things). Now, I'm still puzzled if this is possible from my custom pom.xml file. Maybe I'm missing out on something but I'm still expecting that when I say that my project is depending on spring-orm to have all spring-orm's dependencies downloaded too. Maybe there's some flag or something like that... – Goran Feb 18 '10 at 13:29
  • Or I should just copy-paste dependencies I need from spring-orm's .pom file to my custom pom file??? – Goran Feb 18 '10 at 13:30
  • 1
    When using maven you don't manually download any dependencies (like spring), neither do you hand copy any jars to your projects lib folder. All this is taken care of automatically by maven when you run 'mvn install'. How do you package your application, is it war? – rlovtang Feb 18 '10 at 18:35
  • You should develop your application first and learn maven step by step as needs arise. – Pascal Thivent Feb 19 '10 at 08:29
  • @Pascal: Your answer does not help me very much... I'm trying to wrap my head around Java development/deployment process and if you think that my question is illegitimate you should say so. – Goran Feb 19 '10 at 08:59
  • @Goran cool your tempers, believe me Pascal answers more than 50% question about maven, so no way he is ever going to help you if you speak to him in that way, treat others with respect and others will treat you back(hopefully), he is also golden java specialist, so he can help you a lot in the future, as for your question I updated my answer, hopefully it can be useful to you, what are you trying to build java web application or just java ? – ant Feb 19 '10 at 09:28
  • 1
    @Goran Actually, you are adding **new** questions to the original one which is not a good practice. Your initial question was about adding a spring dependency and has been answered. If you have other questions, start new topics. But IMHO, things like creating a final bundle are more advanced if it's the first time you are using maven and are not blocking for now, this can wait. My point is you should go step by step instead of trying to embrace everything in one move. – Pascal Thivent Feb 19 '10 at 09:31
  • I didn't want to sound disrespectful in any way. My apologies if I sounded that way. – Goran Feb 19 '10 at 09:32
  • @Goran There is no problem with me, I was just trying to help you to get started the best way with maven and with StackOverflow (SO is not a forum or mailing list). My comment was probably not clear enough. – Pascal Thivent Feb 19 '10 at 09:49
  • 1
    I have only built applications packaged as war or ear, so I don't know how you can package only your jar with all dependencies. But I bet there is a maven plugin for that. As a workaround for now you can add a thin war module with dependency to your jar, only for the sake of packaging all dependencies (which you after a 'mvn package' will find in target//WEB-INF/lib/) – rlovtang Feb 19 '10 at 09:55
  • 1
    @rlovtang there is assembly:assembly – ant Feb 19 '10 at 10:29
  • 1
    @Goran http://stackoverflow.com/questions/2287718/spring-3-0-0-dependencies-download-with-maven#answer-2287745 – ant Feb 19 '10 at 10:30
  • @c0mrade thanks, I didn't know about assembly:assembly. Sounds like a much better solution than creating a war. – rlovtang Feb 19 '10 at 14:38

6 Answers6

12

Actually, your POM is wrong, it's missing essential parts. You need at least something like this (this is a minimal POM):

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.myproject</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>My App</name>
</project>

To add a spring dependency, I then suggest to use the following:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.myproject</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>My App</name>

  <properties>
    <org.springframework.version>3.0.0.RELEASE</org.springframework.version>
  </properties>

  <dependencies>

    <!--
        Core utilities used by other modules.
        Define this if you use Spring Utility APIs (org.springframework.core.*/org.springframework.util.*)
    -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>

  </dependencies>
</project>

For a full list of Spring artifacts, have a look at Obtaining Spring 3 Artifacts with Maven and pick up what you need.

Update: Just FYI, there are numerous repository search engines that can help you to find artifacts. This might be helpful if you're not used to Maven. Also note that you can get some IDE support for this (Idea, Eclipse, NetBeans, I think they all offer a repository search feature). Actually, in your case I'd suggest to use SpringSource Tools Suite (STS) which is an integrated version of Eclipse (bundling some plugins for Spring projects development, including Maven support). STS is a fully integrated environment, very close to what you can get in the .NET world IMO. You'll like it.

Community
  • 1
  • 1
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • I paste all that in my pom.xml and still get NullPointerException... People please keep in mind that I only have 1 day of JAVA experience. – Goran Feb 18 '10 at 10:34
  • 1
    @Pascal: He's new to Java, Maven & Spring.. Maybe add the ${org.springframework.version} property in your example, or replace it with the actual version.. He's confused enough as it is.. ;) – Tim Feb 18 '10 at 10:49
  • 2
    @Tim I've done that (and as the link I'm providing is using this property, I prefer to use it, which is a good thing anyway if you need many of their artifacts). – Pascal Thivent Feb 18 '10 at 11:18
  • Thank you a lot... You've been a great help. – Goran Feb 19 '10 at 13:03
2

Just put this in your pom.xml

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>3.0.0.RELEASE</version>
        </dependency>

There is several maven repositories where from you can download libraries , here is one

http://maven.nuxeo.org/nexus/index.html#welcome

This should work if there is that particular version of spring on the repository, I currently use 2.5.5

UPDATE

You see, I don't want to deploy my JAR file with pom.xml only, but all the JARs that are needed for my application to run.

Then you need something called assembly plugin, which will make .jar with dependencies for you, you remember the book I gave link to, search assembly keyword to find out more. But here is how I do it with assembly :

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>

mvn clean install assembly:assembly

That is if you want .jar then you need to specify <packaging>jar</packaging>

ant
  • 22,634
  • 36
  • 132
  • 182
2

As of Spring 3 there is no longer one big superjar. You should pick the parts you need instead. You need at least:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.0.0.RELEASE</version>
</dependency>
Kees de Kooter
  • 7,078
  • 5
  • 38
  • 45
1
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.my.application</groupId>
  <artifactId>slickapp</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>My application</name>
  <inceptionYear>2010</inceptionYear>


  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.4.2</version>
        <configuration>
          <argLine>-Xmx512m -XX:MaxPermSize=256M -XX:PermSize=20M</argLine>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.2</version>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
          <fork>true</fork>
          <meminitial>128m</meminitial>
          <maxmem>512m</maxmem>
        </configuration>
      </plugin>
    </plugins>
  </build>
    <dependencies>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
        <exclusions>
          <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
        <exclusions>
          <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
        <exclusions>
          <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
        <exclusions>
          <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <exclusions>
          <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
        <exclusions>
          <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
        <exclusions>
          <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
        <exclusions>
          <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
        <exclusions>
          <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
        <exclusions>
          <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
        <exclusions>
          <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.5.3</version>
      </dependency>
      <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
        <scope>provided</scope>
      </dependency>

      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.7</version>
        <scope>test</scope>
      </dependency>
    </dependencies>

  <properties>
    <spring.version>3.0.0.RELEASE</spring.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>
gicappa
  • 4,862
  • 1
  • 19
  • 23
  • 1
    I'm in a hurry so I can't add a lot of explanation and I just did some cut and paste of something I already have written. Sorry about that. You have to change the infos contained in the tag groupId artifactId and packaging (that could be a "war" for a war file instead of a jar file or "pom" for a multi module project). You should strip away all the dependencies that are unnecessary to you. – gicappa Feb 18 '10 at 10:52
0

I found a great exaple of pom.xml and it work's fine for me.

Here's the link Spring 3 Hibernate 4 Pom.xml

Ravi Kant
  • 4,785
  • 2
  • 24
  • 23
0

Ok. I got this to work:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-orm</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

And I see various jars being downloaded, but still have no clue about where are they downloaded to.

Also I see various spring jars (for example spring-jdbc-3.0.0.RELEASE.jar) being downloaded, but where is Hibernate? Shouldn't Hibernate be downloaded too???

Goran
  • 1,807
  • 7
  • 27
  • 41
  • 1
    @Goran they are being downloaded to your local repository, in .m2 folder of your user root folder, well as someone mentioned the spring is not a big jar file anymore, you need to choose which dependencies you need, then include them in your pom to be downloaded to your local repository, I suggest you take a glimpse on this book its not very long but useful, just go trough few chapters and you'll figure out what it is all about just after reading 2nd chapter I promise .. here it is http://www.sonatype.com/books/mvnex-book/reference/public-book.html – ant Feb 18 '10 at 11:37
  • 1
    @Goran also instead of adding new answers, its common practice here on SO to edit your posts not adding new answers unless you want to answer your own question which is encouraged – ant Feb 18 '10 at 11:37
  • @c0mrade: Reading the book now... But if I understood you correctly, I need to state Hibernate dependency separately from spring-orm in my pom file. All this time I thought that Hibernate and all other spring-orm "sub dependencies" (to call them that way) will be automatically downloaded when I state spring-orm dependency alone in the pom file. – Goran Feb 18 '10 at 11:42
  • 1
    @Goran I think as of spring 3.0.0. they are considered to be optional dependencies so they need to be included separately each dependency, this is just a guess I'm not sure about this, if Pascal or someone more experienced confirms my statement then its true, so far I've worked with spring 2.5.5 – ant Feb 18 '10 at 11:54