Maven 不复制依赖项

Posted

技术标签:

【中文标题】Maven 不复制依赖项【英文标题】:Maven doesn't copy dependencies 【发布时间】:2013-11-25 06:08:18 【问题描述】:

我正在使用 maven-jar-plugin 和 maven-dependency-plugin 期望依赖的 jar 会被复制到我的 jar 文件中的 lib 文件夹中,但这不会发生。 (但是,jar 被复制到 jar 外部的 target/lib 中,但从未复制到 jar 中。并且依赖项被添加到清单中的类路径中)。我这里有什么问题吗?谢谢你的帮助。

 <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>abc</groupId>
   <artifactId>SendXmlJms</artifactId>
   <packaging>jar</packaging>
   <version>1.0</version>
   <name>SendXmlJms</name>
   <description>send jms message</description>
   <build>
    <sourceDirectory>src</sourceDirectory>
     <resources>
       <resource>
        <directory>src</directory>
         <excludes>
          <exclude>**/*.java</exclude>
         </excludes>
      </resource>
     </resources>

    <plugins>
       <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
          <configuration>
          <source>1.6</source>
          <target>1.6</target>
          </configuration>
       </plugin>
       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib</classpathPrefix>
              <mainClass>SendJms</mainClass>
            </manifest>
          </archive>
         </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-dependency-plugin</artifactId>
           <version>2.1</version>
          <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
           </goals>
           <configuration>
               <includeGroupIds>org.springframework</includeGroupIds>
               <outputDirectory>$project.build.directory/lib</outputDirectory>
               <overWriteReleases>false</overWriteReleases>
               <overWriteSnapshots>false</overWriteSnapshots>
               <overWriteIfNewer>true</overWriteIfNewer>
           </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>

  </build>

  <repositories>
    <repository>
      <id>java.net-m2</id>
       <name>java.net - Maven 2</name>
        <url>https://repository.jboss.org/nexus/content/repositories/java.net-m2</url>
     </repository>
   </repositories>  

   <properties>

    <!-- Generic properties -->
    <java.version>1.6</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <!-- Spring -->
    <org.springframework.version>3.2.4.RELEASE</org.springframework.version>

    <!-- Hibernate / JPA -->
    <hibernate.version>4.2.1.Final</hibernate.version>

    <!-- Logging -->
    <logback.version>1.0.13</logback.version>
    <slf4j.version>1.7.5</slf4j.version>

    <!-- Test -->
    <junit.version>4.11</junit.version>

</properties>

<dependencies>

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

    <!-- Logging with SLF4J & LogBack -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>$slf4j.version</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>$logback.version</version>
        <scope>runtime</scope>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>$hibernate.version</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.1.5.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.2.6</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-oxm</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>$org.springframework.version</version>
    </dependency>
<dependency>
        <groupId>com.sun.messaging.mq</groupId>
        <artifactId>fscontext</artifactId>
        <version>4.2</version>
    </dependency>
   <dependency>
        <groupId>jboss</groupId>
        <artifactId>jboss-j2ee</artifactId>
        <version>3.2.1</version>
  </dependency>
    <!-- Test Artifacts -->
    <dependency>
          <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>$org.springframework.version</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>$junit.version</version>
        <scope>test</scope>
    </dependency>
       </dependencies>  
     </project>

【问题讨论】:

试试 maven Assembly 插件或 shade 插件。 Booth 能够构建 On-Jar-Applications。 【参考方案1】:

我遇到问题的 jar 包含一个模块,该模块将在以后的另一个 maven 构建中使用。我稍后在具有系统范围的本地文件系统上引用它。系统范围工件不会被复制到运行时 jar。

与使用组装插件不同,一种更简单的解决方案(也是 maven 项目文档推荐的解决方案)是在本地安装组件模块,然后稍后引用它。

我在组件构建结束时使用 exec-maven-plugin 执行此操作。

【讨论】:

以上是关于Maven 不复制依赖项的主要内容,如果未能解决你的问题,请参考以下文章

获取最新的 Maven 依赖项。 [复制]

如何通过Gradle将所有依赖项复制到Maven存储库?

如何在 Eclipse 中启用索引下载以进行 Maven 依赖项搜索? [复制]

使 Maven 将依赖项复制到 target/lib

Maven复制依赖项

如何使用maven依赖插件将类型为pom的依赖项复制到一个位置