Maven 生成 POM 文件

38
我使用的是maven 3.0.3,尝试生成第三方jar包的pom文件,如下所示:

mvn install:install-file -Dfile=cobra.jar -DgroupId=com.cobra -DartifactId=cobra -Dversion=0.98.4 -Dpackaging=jar -DgeneratePom=true

根据下面的链接,它应该生成正确的pom.xml并将构件安装到repo中。 http://maven.apache.org/plugins/maven-install-plugin/examples/generic-pom-generation.html 但同时,它返回了以下错误:

[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (D:\cobra-0.98.4\lib). Please verify you invoked Maven from the correct directory. -> [Help 1]

为什么会要求pom.xml而不是生成pom.xml呢?

我刚在Windows 7的cmd中运行了这条命令,对于Maven 3.0.5它是有效的。也许他们在这个版本中修复了它。 - firen
10个回答

101

这是一个老问题,但对我来说仍然非常麻烦,所以我想分享一下:

我刚遇到了这个问题,我认为问题可能是依赖于平台的。真正的提示是Cyril的答案并没有像预期那样工作:尽管我在命令行上指定了-DgroupId=com.xyz-DartifactId=whatever,并在POM文件中相应地设置了条目,但该jar包被安装在本地库的位置变成了com/whatever

这促使我尝试引用命令行参数,最终正确的结果是将命令行格式化为这样(删除POM文件后):

mvn install:install-file "-Dfile=cobra.jar" "-DgroupId=com.cobra" "-DartifactId=cobra" "-Dversion=0.98.4" "-Dpackaging=jar" "-DgeneratePom=true"

引用可能有些冗余,但谨慎起见还是保险一点,对吧?我这台电脑上运行着Vista系统,如果这个问题只出现在这个操作系统版本上也不会感到惊讶。顺便说一句,使用的是Maven v3.0.4。


13
这个错误信息"您指定的目标需要一个项目来执行..."真是无用之至。 - David Soroko
6
在使用控制台时,这对我有用。在不加引号的情况下,CMD按预期工作。 - flesk
1
与flesk所写的类似,对于Windows系统,我在powershell中遇到了这个错误,而不是在cmd中。感谢flesk。 - Levin Magruder
2
作为额外的澄清,这是点号破坏了PowerShell解析。您只需要引用其中带有点号的参数即可。 - Alex N.
哇,我刚发现这个问题,感到非常生气。 - Riv
1
你的“cobra.jar”文件里有什么? - jamiel22

8

你确定正在执行install-file目标吗?我检查了你的命令,它对我起作用,但是当我放置一个空的install :install-file(也许你有这个笔误)时,将使用install目标,该目标需要pom.xml。

尝试使用-X参数获取更多调试信息:

 -X,--debug       Produce execution debug output

我的系统
Maven
c:\>mvn -version

Apache Maven 3.0.3 (r1075438; 2011-02-28 18:31:09+0100)
Maven home: C:\progs\apache-maven-3.0.3
Java version: 1.6.0_21, vendor: Sun Microsystems Inc.
Java home: c:\Program Files (x86)\Java\jdk1.6.0_21\jre
Default locale: de_DE, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"

安装插件

c:\>mvn -Dplugin=install help:describe

Name: Maven Install Plugin
Description: Copies the project artifacts to the user's local repository.
Group Id: org.apache.maven.plugins
Artifact Id: maven-install-plugin
Version: 2.3.1
Goal Prefix: install

This plugin has 3 goals:

install:help
  Description: Display help information on maven-install-plugin.
    Call
      mvn install:help -Ddetail=true -Dgoal=<goal-name>
    to display parameter details.

install:install
  Description: Installs the project's main artifact in the local repository.

install:install-file
  Description: Installs a file in the local repository.

For more information, run 'mvn help:describe [...] -Ddetail'

没有这样的拼写错误。你能告诉我你使用的Maven版本吗? - firen
我有完全相同的版本... 真的不知道,也许我会调试Maven代码并查看那里发生了什么。 - firen

5
我发现了一种绕过的方法。您需要创建一个像这样简单的pom.xml文件:
<project 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.cobra</groupId>
    <artifactId>cobra</artifactId>
    <version>0.98.4</version>
</project>

虽然不是完美的解决方案,但对我来说很有效。如果你找到更好的方法,请告诉我。

我的配置:

$mvn -version
Apache Maven 3.0.3 (r1075438; 2011-02-28 18:31:09+0100)
Maven home: /usr/local/maven
Java version: 1.6.0_20, vendor: Sun Microsystems Inc.
Java home: /usr/local/jdk1.6.0_20/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-25-generic-pae", arch: "i386", family: "unix"

$mvn -Dplugin=install help:describe
...
Name: Maven Install Plugin
Description: Copies the project artifacts to the user's local repository.
Group Id: org.apache.maven.plugins
Artifact Id: maven-install-plugin
Version: 2.3.1
Goal Prefix: install
...

2

使用 Maven 版本 3.6.3,以下方法可帮助我在所选目录中生成 pom 文件:

C:<MyChosenDir> mvn archetype:generate "-DgroupId=com.mycompany.app" "-DartifactId=my-app" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.4" "-DinteractiveMode=false"

1
尝试在执行mvn命令之前在cmd.exe上运行它或执行cmd命令。

我曾经遇到过同样的问题,这个方法可以解决。 - Sin2

0
当我在Windows上使用Maven时,遇到了这个错误。对我有效的方法是打开cmd而不是PowerShell。 显然,如果你不用cmd,会出现某些错误。 希望它能解决问题。

1
请提供更多问题细节,例如添加错误或代码等。 - Tejal

0
如果您在Jenkins管道设置期间遇到此错误,则错误是我们在Jenkins git URL空白处放置了没有.git的git存储库。通常,我们会将git repo的网站URL放入其中,但我们需要放置git repo的克隆URL。将.git(克隆URL)插入网站URL中,只需插入git clone URL即可。

目前你的回答不够清晰,请编辑并添加更多细节,以帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

0

只需进入您的项目目录,在那里您可以找到您的pom.xml文件,然后执行相同的命令!这对我有效;)


0
如果您在使用Maven的命令行版本时遇到了问题,您可以尝试使用Eclipse的M2E插件。对于没有太多Maven经验的人来说,它更加用户友好。

0

当我将 Powershell 更改为 Cygwin 时,它对我起作用了。Powershell 在某种程度上会错误地解析命令行参数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接