Java 使用 Maven 下载 Spring 3.0.0 依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2287718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:41:15  来源:igfitidea点击:

Spring 3.0.0 dependencies download with Maven

javaspringmaven-2dependencies

提问by Goran

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

我只是从 .NET 过渡到 JAVA,并且必须使用 Spring 3.0.0 启动 JAVA 项目。和休眠。

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.

有人可以向我解释 step_by_step 如何使用 Maven 下载 spring 依赖项。我花了一整天的时间,完全没有成功。

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

附注。我有 Java 1.5.06 并且已经下载了 spring(没有依赖项)并安装了 Maven。

edit:

编辑:

c0mrade:

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

我认为从 spring 3.0.0 开始。它们被认为是可选的依赖项,因此它们需要单独包含在每个依赖项中,这只是一个猜测,我对此不确定,如果 Pascal 或更有经验的人证实了我的陈述,那么它是正确的,到目前为止我已经与弹簧 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.

是的......它们绝对是可选的,所以这就是我所做的。我只是将 spring-orm pom 文件中的 hibernate 依赖项复制/粘贴到 myproject pom 文件中,这意味着现在我在 pom 文件中定义了 spring 和 hibernate 依赖项。然后我在 myproject 上运行了“mvn install”,然后将所有 spring 和 hibernate jar 手动复制到我的项目的 lib 文件夹中。

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

所以现在我有一个带有 spring 和 hibernate 的 Java 项目。:)

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

我正在学习 Java,这只是我的第二天,所以如果我做错了什么,请告诉我。

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?

使用 maven 时,您不会手动下载任何依赖项(如 spring),也不会将任何 jar 手动复制到您的项目 lib 文件夹中。当您运行“mvn install”时,所有这些都由 maven 自动处理。你如何打包你的应用程序,是战争吗?

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.

我明白那个。我很清楚 Maven 自动管理我本地存储库中依赖项的类路径,因此我的项目可以在我的本地机器上正常工作。我还表示您可以选择将依赖项 jar 打包到 WAR 或 EAR 中,但是如果我想将我的应用程序打包为 JAR 以及输出(目标)文件夹中的所有依赖项 JAR,该怎么办?你看,我不想只用 pom.xml 部署我的 JAR 文件,而是我的应用程序运行所需的所有 JAR。



It still does not work:

它仍然不起作用:

this is my pom.xml:

这是我的 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:

当我点击“mvn install”时,出现此异常:

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

[INFO] 正在扫描项目... [INFO] -------------------------------------- ---------------------------------- [错误] 致命错误 [信息] -------- -------------------------------------------------- -------------- [INFO] null [INFO] ----------------------------- ------------------------------------- [INFO] 跟踪 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) 在 org.apache.maven.project.artifact.ProjectArtifactFactory.create(ProjectArtifactFactory.java:37) 在 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) ) 在 org.apache.maven.DefaultMaven.getProjects(DefaultMaven.java:391) 在 org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:272) 在 org.apache.maven.DefaultMaven.execute(DefaultMaven.java: 138) 在 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。 org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315) 的 java.lang.reflect.Method.invoke(Unknown Source) at org.codehaus.classworlds.Launcher.launch (Launcher.java:255) 在 org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430) 在 org.codehaus.classworlds.Launcher.main(Launcher.java:375) [INFO] ------ -------------------------------------------------- ---------------- [INFO] 总时间:< 1 秒 [INFO] 完成时间:Thu Feb 18 11:19:44 CET 2010 [INFO] 最终记忆:1M/2M [信息] -------------------------------------------- -----------------------------

采纳答案by Pascal Thivent

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

实际上,您的 POM 是错误的,它缺少必要的部分。你至少需要这样的东西(这是一个最小的 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:

要添加 spring 依赖项,我建议使用以下内容:

<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 Mavenand pick up what you need.

有关 Spring 工件的完整列表,请查看使用 Maven 获取 Spring 3 工件并选择您需要的内容。

Update:Just FYI, there are numerous repository search enginesthat 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.

更新:仅供参考,有许多存储库搜索引擎可以帮助您找到工件。如果您不习惯 Maven,这可能会有所帮助。另请注意,您可以获得一些 IDE 支持(Idea、Eclipse、NetBeans,我认为它们都提供了存储库搜索功能)。实际上,在您的情况下,我建议使用SpringSource Tools Suite(STS),它是 Eclipse 的集成版本(捆绑了一些用于 Spring 项目开发的插件,包括 Maven 支持)。STS 是一个完全集成的环境,非常接近您在 .NET 世界 IMO 中可以获得的环境。你会喜欢的。

回答by ant

Just put this in your pom.xml

把它放在你的 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

有几个 Maven 存储库,您可以从中下载库,这里是一个

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

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

如果存储库中有特定版本的 spring,这应该可以工作,我目前使用 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.

你看,我不想只用 pom.xml 部署我的 JAR 文件,而是我的应用程序运行所需的所有 JAR。

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 :

然后你需要一个叫做 assembly plugin 的东西,它会为你制作带有依赖项的 .jar,你记得我给链接的那本书,搜索 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>

也就是说,如果你想要 .jar 那么你需要指定 <packaging>jar</packaging>

回答by Kees de Kooter

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

从 Spring 3 开始,不再有一个大的超级罐子。你应该选择你需要的部分。你至少需要:

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

回答by gicappa

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

回答by Goran

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 whereare they downloaded to.

我看到各种 jars 正在下载,但仍然不知道它们下载哪里

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

我还看到正在下载各种 spring jar(例如 spring-jdbc-3.0.0.RELEASE.jar),但是 Hibernate 在哪里?不应该也下载Hibernate吗???

回答by Ravi Kant

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

我找到了 pom.xml 的一个很好的例子,它对我来说很好用。

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

这是Spring 3 Hibernate 4 Pom.xml的链接