【问题标题】:Create a jar that contain dependency management maven [duplicate]创建一个包含依赖管理 maven 的 jar [重复]
【发布时间】:2017-06-14 15:41:28
【问题描述】:

我已经创建了一个 jar,其中包含一些常见的 jar,我们将在不同的应用程序中使用它,并且可以被许多用户使用。

为了解决这个问题,我创建了一个通用 jars,这样我就可以控制这个 commons jars 和它们的版本,然后将它存储在 nexus 服务器中,这样它就可以包含父 pom 并且可以被许多开发人员使用。

所以我创建了一个简单的 maven java 应用程序,并在这个 jar 的 pom 中,例如:

父 pom

<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>com.emp</groupId>
    <artifactId>dependencymanager</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.primefaces</groupId>
                <artifactId>primefaces</artifactId>
                <version>6.0</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>javax.persistence</artifactId>
                <version>2.1.1</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

然后在子应用程序中我像这样使用这个 jar:

<dependencies>
    <dependency>
        <groupId>com.emp</groupId>
        <artifactId>dependencymanager</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

但是当我尝试使用org.primefaces 时,我不能。我哪里错了,还是我在依赖管理的实现上错了?

注意

我已经读过这个:http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

所以在本教程的示例中,它没有指定父 pom 的版本,所以当我删除它时会显示如下错误:

'dependencies.dependency.version' for com.emp:dependencymanager:jar is missing.

问题

  • 在这种情况下如何实现依赖管理?
  • 有没有比这更好的方法来处理相同的依赖项 许多项目和许多开发人员。
  • 我使用 nexus 来存储我的罐子

谢谢。

【问题讨论】:

    标签: java maven jar dependency-management


    【解决方案1】:

    我使用Maven Shade plugin 在生成的jar 文件中具有依赖关系。检查类似的代码是否适合您

    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.4.3</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
    </build>
    

    【讨论】:

      【解决方案2】:

      在父pom中,直接添加dependencies,省略dependencyManagement标签。使用dependencyManagement,您只需指定如果在您的 pom 中声明了此类依赖项,则使用父 pom 中指定的版本。但是您的父 pom 本身并不包含这些依赖项作为传递依赖项。

      【讨论】:

      • 谢谢@cello,当我删除 de dependencyManagement 时它工作正常,这部分已解决,那么如果我更改 primefaces 的版本,然后将这个 jar 存储在 nexus 中可以所有用户的版本都更改了吗?
      • 只要用户不自己用其他版本添加primefaces,就会使用父pom中指定的版本。
      • 那么为什么人们会谈论 dependencyManagement 我如何、何时何地使用它?
      • dependencyManagement 部分用于指定依赖项的版本,何时使用它们。在“子”pom 中,可以只添加依赖项的组和工件 id,而忽略版本。然后将从父 pom 的 dependencyManagement 部分获取版本。
      • 所以这就是我想要做的,因为我用dependencyManagement section 创建了这个 jar 来使用这个版本而不是另一个版本,但我认为这不起作用,我很抱歉我的问题
      猜你喜欢
      • 2014-01-08
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多