【问题标题】:Project depedencies in Eclipse m2e multiproject vs. package with manifestEclipse m2e 多项目中的项目依赖项与带有清单的包
【发布时间】:2019-02-06 17:14:31
【问题描述】:

我正在完成将独立的多项目 java 从 Ant 迁移到 Maven。我的项目的结构类似于以下:

parent-project
|_ Project A
|  |
|_Project B
|_Project C

在父项目中没有代码,但我在 DependencyManager 中定义了最常用的依赖项及其版本。 ProjectA POM 也使用 parent-project 和 ProjectB 中定义的依赖关系,并从 ProjectA 调用方法。

我可以从 Eclipse 中完美地执行我的桌面应用程序作为 Maven 项目(目标:exec:java)。 m2e 提供的 Dependency Hierarchy 选项卡显示了正确的依赖顺序,与 dependency:build-classpath 显示的相同。

dependency:build-classpath tells Maven to output the path of the dependencies from the local repository in a classpath format to be used in java -cp. The classpath file may also be attached and installed/deployed along with the main artifact.

然而,尽管当我从 Eclipse 将独立应用程序作为 Maven 主项目启动时,我的项目依赖似乎是正确的,但当我将 jar 及其 jar 依赖项打包到子目录中时(使用 maven-jar 创建-插入)。

MyApp.jar
|_ \lib
   |_ MyApp dependencies 
   |_ Maven projects jars
   |_ Maven projects jars dependencies

我的清单文件包含一个等效于执行“mvn dependency:resolve”的类路径,但我仍然无法弄清楚这些 jar 的顺序来自哪里(即使更改 POM 似乎也没有对它有任何影响)。我的应用程序设法运行,但运行时库有很多问题,因为它没有使用它应该使用的那个。

dependency:resolve tells Maven to resolve all dependencies and displays the version

如果有人能指出这个问题的根源,我会永远感激不尽。

【问题讨论】:

  • 你能更清楚你想要实现的目标吗?您是想构建一个包含所有依赖项的“胖罐子”,还是想通过路径引用它们?
  • 我编辑了这个问题,试图提供更多细节。感谢您感兴趣。
  • 你有重叠类的罐子吗?这是您需要在类路径上的 jar 中进行特殊排序的原因吗?
  • 某些依赖项包括它们自己的依赖项,这些依赖项可能会在运行时发生冲突(例如:cxf、axis 和 jdk webservices)。但是,对我来说,谜团仍然是这种依赖关系:解决顺序与依赖关系:构建类路径的差异。这反映了我的清单的结果,因此也反映了打包后的应用程序依赖项。

标签: java eclipse maven


【解决方案1】:

毕竟我自己找到了解决办法。

该问题仅在调用maven-dependency-plugin 复制依赖项时出现。我删除了该插件的执行并将复制资源替换为maven-resources-plugin

之前(不工作):

  <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>jar-with-dependencies</id>
    <formats>
        <format>dir</format> 
    </formats>
    <!-- Dependencies properties -->
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <useProjectArtifact>false</useProjectArtifact>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>
    <!-- To avoid include the target name as a root directory -->
    <includeBaseDirectory>false</includeBaseDirectory>

    <fileSets>
        <!-- Copy resources from resources to target -->
        <fileSet>
            <directory>../AnotherJavaProject/src/main/resources/utils/</directory>
            <outputDirectory>utils</outputDirectory>
        </fileSet>
        <!-- ...more resources here -->

        <!-- Copy the .exe to the target folder -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.exe</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

之后(工作):

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>package</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
                <resources>
                    <resource>
                    <!-- Copy resources from resources to target -->    <directory>../AnotherJavaProject/src/main/resources/utils/</directory>
                        <targetPath>utils</targetPath>
                    </resource>
                    <!-- ...more resources here -->

                    <!-- Copy the .exe to the target folder -->
                    <resource>
                        <directory>${project.build.directory}</directory>
                        <include>*.exe</include>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2012-01-25
    • 2016-08-06
    • 2020-09-09
    • 2012-12-17
    • 2010-12-14
    • 2015-12-24
    • 2013-06-22
    • 1970-01-01
    • 2011-02-21
    相关资源
    最近更新 更多