【问题标题】:Maven dependencies not being compiled未编译 Maven 依赖项
【发布时间】:2014-03-22 07:51:36
【问题描述】:

过去一个小时左右,我一直在尝试让我的 Maven 项目包含来自其依赖项的源文件,但由于某种原因,它不是。我已按照以下链接提供的步骤进行操作,但是当我编译并运行插件时,我得到了 ClassNotFoundException:

https://github.com/mkremins/fanciful

我已确保将上面链接中的依赖项和存储库包含到我的 pom.xml 文件中,但是当我编译时,它们不会被添加到我的 .jar 文件中。

我对使用 Maven 还很陌生,到目前为止我很喜欢它,尽管解决这样的问题可能会很痛苦。

我正在通过执行以下操作来构建项目:

右键项目 -> 运行方式 -> Maven 构建 -> 目标:全新安装

  • 编辑-

经过一番搜索,我发现这并不像我想象的那么容易。我将以下内容添加到我的 pom.xml 构建部分:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.1</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <createDependencyReducedPom>false</createDependencyReducedPom>
          <minimizeJar>true</minimizeJar>
          <artifactSet>
              <includes>
                  <include>mkremins:fanciful</include>
                  <include>org.json:json</include>
              </includes>
          </artifactSet>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

唯一的问题是我还需要手动包含我想使用的主库的依赖项 - mkremins:fanciful;是否有标志或选项可以自动从我需要的一个文件中复制依赖项,而不是包括&lt;include&gt;org.json:json&lt;/include&gt;

【问题讨论】:

  • 我建议将此问题的名称更改为“未打包的 Maven 依赖项”。简而言之:编译 -> 从 .java 打包创建 .class -> 创建 .jar(或其他归档格式)

标签: java maven dependencies repository


【解决方案1】:

你误解了Maven。 Maven 旨在使用位于 Maven 中心的依赖项。它的想法不是编译依赖项。我建议您阅读 about Maven 并了解其工作原理。

【讨论】:

    【解决方案2】:

    好吧,如果你想将你的依赖项复制到你的目标 jar 中,你需要告诉 maven 这样做! Maven 不知道您的项目的工件是自给自足的可执行 jar、要在容器内执行的 jar,还是只是另一个项目的依赖项或库。

    您可能想要使用来自maven-dependency-plugin 的复制依赖项任务

    例如:

        <build>
    
                <plugins>
    
                    <plugin>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-dependencies</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>copy-dependencies</goal>
                                </goals>
                                <configuration>
                                    <includeScope>runtime</includeScope>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
    
                                    <excludeTransitive>false</excludeTransitive>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
    </plugins>
    </build>
    

    要进行更多调整,您可能还想使用 jar 插件和程序集插件。关于创建可执行 jar 的更多信息:

    http://www.ibm.com/developerworks/java/library/j-5things13/index.html?ca=dat-

    【讨论】:

    • 我昨晚睡前想出了这个问题——但我的插件部分与你的有点不同,因为我只需要两个特定的依赖项。第一个依赖是我正在使用的库,而第二个依赖是第一个依赖所需的 JSON 库;有没有一种简单的方法来包含依赖项的依赖项?
    • dependencies of dependencies == transitive dependencies 默认情况下,copy-dependencies 也会复制它们,为了更加冗长,我只是将此元素显式设置为 false
    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 2011-04-15
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    相关资源
    最近更新 更多