【问题标题】:Maven does not read POM when installing JAR to repository将 JAR 安装到存储库时,Maven 不读取 POM
【发布时间】:2014-12-03 05:31:21
【问题描述】:

我已按照指南 here 将 JAR 文件安装到我的本地存储库中。

我运行以下命令:

mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=log4j-weblayout-0.0.1-SNAPSHOT.jar

JAR 文件是使用 Maven 构建的,其中包含一个 POM 文件,其中列出了其依赖项。 JAR里面的文件有路径:

/META-INF/maven/in.ksharma/log4j-weblayout/pom.xml

Maven 安装工件但不读取其 POM。它会创建一个没有任何依赖信息的空 POM 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>in.ksharma</groupId>
  <artifactId>log4j-weblayout</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <description>POM was created from install:install-file</description>
</project>

如何确保 JAR 中的 POM 是安装的那个?

编辑:

JAR中各种文件的内容如下。

/META-INF/MANIFEST.MF:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: kshitiz
Created-By: Apache Maven 3.1.0
Build-Jdk: 1.8.0

/META-INF/maven/in.ksharma/log4j-weblayout/pom.properties:

#Generated by Maven
#Wed Oct 08 19:48:28 IST 2014
version=0.0.1-SNAPSHOT
groupId=in.ksharma
artifactId=log4j-weblayout

/META-INF/maven/in.ksharma/log4j-weblayout/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>
    <groupId>in.ksharma</groupId>
    <artifactId>log4j-weblayout</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <url>https://github.com/Kshitiz-Sharma/log4j-weblayout</url>
    <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
    </properties>
    <dependencies>
        <!-- Compile dependencies -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>joor</artifactId>
            <version>0.9.3</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
            <scope>compile</scope>
        </dependency>

        <!-- Provided dependencies -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>provided</scope>
        </dependency>

        <!-- Test dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.5.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.1.8</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>

【问题讨论】:

  • 你能把罐子里的pom的内容贴出来吗?
  • @AndrésMongeMoreno 请查看我的编辑。
  • 问题是:您有一个现有的 pom 可用于构建工件...为什么不简单地做:mvn install
  • @khmarbaise 如果我想将构建的 JAR 而不是代码分发给其他用户怎么办?
  • 如果您进行安装,它只会将 jar 文件安装到本地存储库中。那么这里有什么问题呢? (要清楚,可以简单地提取 jar 文件并可以反编译类文件)...我没有看到问题?

标签: maven maven-3 pom.xml maven-install-plugin


【解决方案1】:

我认为您需要从 jar 中提取 pom.xml 并使用 pomFile property 指定它,如下所示:

mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=log4j-weblayout-0.0.1-SNAPSHOT.jar -DpomFile=pom.xml

【讨论】:

  • 我认为没有必要。文档状态If the JAR was built by Apache Maven, it'll contain a pom.xml in a subfolder of the META-INF directory, which will be read by default.
  • 我在 install-file 目标的文档中没有看到:maven.apache.org/plugins/maven-install-plugin/…。有几个选项表明可以从包含的 pom.xml 中提取 groupId、artifactId 和 version,但是我没有看到您在此处复制的句子。
  • 这句话是从问题中提供的link复制而来的。
  • 这是正确答案。尽管 Maven 安装插件应该能够使用 JAR 中的 pom,但正如 joanpau 已经提到的,当前版本 (2.5.2) 有一个 bug 并且没有这样做。
【解决方案2】:

似乎在安装插件的 3.0.0-M1 版本中已修复,但 maven 可能仍使用 2.5.2 作为默认值。您可以在 pluginManagement 部分设置安装插件的版本:

  <pluginManagement>
      <plugins>
      ...
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-install-plugin</artifactId>
          <version>3.0.0-M1</version>
        </plugin>
      ...
      </plugins>
  </pluginManagement>

或者在命令行调用正确版本的插件:

mvn org.apache.maven.plugins:maven-install-plugin:3.0.0-M1:install-file -Dfile=log4j-weblayout-0.0.1-SNAPSHOT.jar

【讨论】:

    【解决方案3】:

    好像是already fixed bug(2015 年 1 月), 尽管该修复不在最新版本中(根据plugins official site,2.5.2 版于 2014 年 8 月 27 日发布)

    【讨论】:

    • 理论上,这个 bug 已经修复,并且在 3.0.0 中修复发布,但一年后的 3.3.3 中我仍然看到同样的问题。
    • 我在 3.5.2 中看到了同样的错误。
    • 6 年多之后,这仍然是 3.6.3 中的一个问题。我猜这是由于@JensViebig 在他的回答中所说的话。为什么 Maven 仍然默认使用带有此错误的 maven-install-plugin 版本?!?我看到插件版本已在已链接到的文档中更新,但未在 this page 上更新。 :'(
    • 我也希望有人提到应该检查本地存储库中安装的pom.xml 是否正确。我花了比我愿意承认的时间更长的时间来确定这是缺少依赖项问题的根本原因。人们通常只安装具有各自依赖项的 jar,还是我错过了什么? Here 是另一个页面,显示了 install-file 命令,几乎没有其他信息。
    猜你喜欢
    • 2015-05-15
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多