Java 如何从 maven pom 文件构建项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4173000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:19:35  来源:igfitidea点击:

How to build project from maven pom file

javamaven

提问by Alan McCloud

I have a maven pom file for an open source project. This pom file has all the info like what other jars it depends on etc. I installed maven. Created a dir samprj and copied the pom file into that dir. Cd into that dir and ran mvn command without any arguments but I got bunch of errors. I am absolutely new to maven so I think I am missing something. I tried also from Eclipse ( Import project -- exisitng maven project) but that also does not work except eclipse creates a project that has just that file pom.xml. I expect something that first it will download the jar for the project and then download all dependent jars and config files but nothing there. So given a pom file how do I build the project from it?

我有一个开源项目的 maven pom 文件。这个 pom 文件包含所有信息,比如它依赖的其他 jars 等。我安装了 maven。创建一个目录 samprj 并将 pom 文件复制到该目录中。Cd 进入该目录并在没有任何参数的情况下运行 mvn 命令,但我遇到了一堆错误。我对 maven 完全陌生,所以我想我错过了一些东西。我也从 Eclipse ( 导入项目 -- exisitng maven 项目) 尝试过,但这也不起作用,除了 Eclipse 创建一个只有该文件 pom.xml 的项目。我希望它首先下载项目的 jar,然后下载所有依赖的 jar 和配置文件,但没有。那么给定一个 pom 文件,我如何从中构建项目?

采纳答案by icyrock.com

If you want to open this within Eclipse, you need to install m2eclipse:

如果你想在 Eclipse 中打开这个,你需要安装 m2eclipse:

and then import the project as a Maven project as described here:

然后将项目作为 Maven 项目导入,如下所述:

回答by JOTN

Try out their getting started guide. It has a lot of good examples:

试试他们的入门指南。它有很多很好的例子:

http://maven.apache.org/guides/getting-started/

http://maven.apache.org/guides/getting-started/

回答by Raghuram

mvn installshould get you going

mvn install应该让你去

回答by Stephen C

I have a maven pom file for an open source project. This pom file has all the info like what other jars it depends on etc. I installed maven. Created a dir samprj and copied the pom file into that dir ...

我有一个开源项目的 maven pom 文件。这个 pom 文件包含所有信息,比如它依赖的其他 jars 等。我安装了 maven。创建一个目录 samprj 并将 pom 文件复制到该目录中...

It sounds like you only have the project's POM file. This is not sufficient. You need to checkout the complete source treefor the project. Having done that, change directory to the directory containing the POM file and run mvn install.

听起来您只有项目的 POM 文件。这还不够。您需要检出项目的完整源代码树。完成后,将目录更改为包含 POM 文件的目录并运行mvn install.

Also, don't copy the POM to a different directory. Maven expects to find all of the source files relative to the POM file location.

另外,不要将 POM 复制到其他目录。Maven 希望找到与 POM 文件位置相关的所有源文件。

FOLLOW UP

跟进

Thanks for advice. I was not able to use the command mvn install as it gave errors.

谢谢你的建议。我无法使用命令 mvn install 因为它给出了错误。

Probably because you hadn't checked out the source.

可能是因为你没有检查出源。

I don't know how to check the source tree of the project ...

不知道怎么查看项目的源码树...

Use a subversion client (the svncommand for example), or one of the Eclipse subversion plugins.

使用 subversion 客户端(svn例如命令)或 Eclipse subversion 插件之一。

If this was a properly documented project, there would be clear instructions on what version control and build tools you needed, how to checkout the source code and how to build it.

如果这是一个正确记录的项目,将会有关于您需要哪些版本控制和构建工具、如何检出源代码以及如何构建它的明确说明。

... as I thought POM itself should have this information to automatically checkout if the source is not check out.

...因为我认为 POM 本身应该具有此信息以在源未检出时自动检出。

It doesn't necessarily, though in this particular case it does.

不一定,尽管在这种特殊情况下确实如此。

Anyway I was able use Eclipse to build the project without errors.

无论如何,我能够使用 Eclipse 构建项目而没有错误。

(Other readers can read @icyrock.com's answerfor links to the m2eclipse plugin and documentation.)

(其他读者可以阅读@icyrock.com 的回答以获取指向 m2eclipse 插件和文档的链接。)

The only problem is the dependent jars were downloaded but hidden deep paths in .m2 repository folder on my linux box.

But I would like these dependent jars to be relative to dir where POM file is.

唯一的问题是依赖的 jars 已下载,但在我的 linux 机器上的 .m2 存储库文件夹中隐藏了深层路径。

但我希望这些依赖的 jars 与 POM 文件所在的目录相关。

Sorry, but that is not the way Maven works.

抱歉,但这不是 Maven 的工作方式。

The ~/.m2/repositorydirectory is a fundamental part of Maven. It is not a problem. It is a feature. (Don't fight it!)

~/.m2/repository目录是 Maven 的基本部分。这不成问题。这是一个特点。(别打了!)