如何在我的 Java 项目中使用 Maven 以及为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3072205/
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-10-30 00:13:42  来源:igfitidea点击:

How to use Maven in my Java Project and Why?

javamaven-2

提问by zengr

I am trying to figure out the use of Mavenand I got many articles describing its features and uses. But I am just not able to understand the actual use of Maven from productivity standpoint.

我试图弄清楚Maven的用法,我收到了很多描述其特性和用途的文章。但我只是无法从生产力的角度理解 Maven 的实际使用。

From what I am used to in our school projects was just create a new Java project in Eclipse, write your Code, create a .war (if web-based) and paste the code to the webapps folder of Tomcat and start the server!

我在学校项目中习惯的只是在 Eclipse 中创建一个新的 Java 项目,编写您的代码,创建一个 .war(如果基于网络)并将代码粘贴到 Tomcat 的 webapps 文件夹中并启动服务器!

So,

所以,

  1. Where does Maven come into picture? I have used Ant and I understand Ants benefit of a standardized build process. But why do we need an advanced Ant in form of Maven?

  2. In any case, I need to use it, so where do I get started - basic flow, some good tutorials?

  1. Maven 在哪里出现?我使用过 Ant 并且我了解 Ants 的标准化构建过程的好处。但是为什么我们需要 Maven 形式的高级 Ant 呢?

  2. 无论如何,我需要使用它,那么我从哪里开始 - 基本流程,一些好的教程?

Thanks

谢谢

采纳答案by Andy

Maven is used to manage the build, testing, and deployment processes. It can separate the unit tests and integration tests so you only run them when necessary and cut down on build time.

Maven 用于管理构建、测试和部署过程。它可以将单元测试和集成测试分开,因此您只在必要时运行它们并减少构建时间。

It is also a dependency manager, which means when you realize the server piece of your project needs apache commons-logging 1.0.4but the client conflicts with anything past 0.7.9, you can just add a couple lines to the respective pom.xmlfiles, and Maven handles all of that (downloading, installing, and keeping track of the different versions of those dependencies).

它也是一个依赖管理器,这意味着当您意识到项目的服务器部分需要apache commons-logging 1.0.4但客户端与过去的任何内容发生冲突时0.7.9,您只需在各自的pom.xml文件中添加几行,Maven 会处理所有这些(下载、安装、并跟踪这些依赖项的不同版本)。

I was not a believer before my current task, but after 2 years using it for large enterprise applications, I definitely respect what Maven brings to the table. There are a lot of online resources but if you are going to be the lead on this and really feel uncomfortable, I recommend getting a book -- the O'Reilly oneis helpful.

在我目前的任务之前,我并不相信,但在将它用于大型企业应用程序 2 年后,我绝对尊重 Maven 带来的东西。有很多在线资源,但是如果您要成为这方面的领导者并且真的感到不舒服,我建议您买一本书——O'Reilly的书很有帮助。



Forgot to mention that there is an Eclipse plugin which makes it almost painless to use with Eclipse: m2Eclipse.

忘了提到有一个 Eclipse 插件,它使与 Eclipse 一起使用几乎无痛:m2Eclipse



Second update for example pom.xmlsegment to answer OP question:

pom.xml回答 OP 问题的示例段的第二次更新:

Your pom.xmlwill contain XML code such as:

pom.xml将包含 XML 代码,例如:

<dependencies>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.0.4</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

These are downloaded from the central Maven repository (google "maven nexus") or you can configure your own additional repositories (like for your own projects, or if you are not Internet-connected).

这些是从中央 Maven 存储库(谷歌“maven nexus”)下载的,或者您可以配置自己的附加存储库(例如用于您自己的项目,或者如果您没有连接到 Internet)。

回答by OscarRyz

I had exactlythe same perception as you and for years I avoided Maven.

我和你的看法完全一样,多年来我一直避免使用 Maven。

The thing is, it allows you to easily get the required jars your application may need( called dependencies - jars and other things - ) . So the next time somebody else run your project he will get the jars automatically.

问题是,它允许您轻松获取应用程序可能需要的所需 jar(称为依赖项 - jar 和其他内容 - )。因此,下次其他人运行您的项目时,他将自动获取罐子。

I know that's a bit hard to grasp, until you work with an existing projects using it.

我知道这有点难以理解,除非您使用它来处理现有项目。

For instance I downloaded an open source project recently, which depended on 10 or 12 different on different jar versions. After downloading the source code and executing Maven, all those jars ( and a lot more others ) were downloaded for me.

比如我最近下载了一个开源项目,它依赖于10或12个不同的jar版本。下载源代码并执行 Maven 后,所有这些 jar(以及更多其他)都为我下载了。

The problem with Maven ( as a friend of mine told me ) is that to perform a "Hello world" program, it first downloads the world to greet him. :P

Maven 的问题(正如我的一个朋友告诉我的那样)是为了执行“Hello world”程序,它首先下载世界来迎接他。:P

回答by Aamir Mushtaq

for all those wondering where the maven downloads the dependency jars, check out a folder named .m2 in the user root directory. eg. for me it is the c:\documentsand settings\myUserName.m2\

对于所有想知道 maven 在哪里下载依赖 jar 的人,请查看用户根目录中名为 .m2 的文件夹。例如。对我来说是 c:\documentsand settings\myUserName.m2\

also i have researched a bit on maven and i have made some small scribbling like reminders. If it is worth a read then here it is ::

我还对 maven 进行了一些研究,并做了一些像提醒之类的小涂鸦。如果它值得一读,那么这里是 ::

/* mvn generate mvn install downloads all necessary jars mvn test tests the application made... mvn site builds the site downloading dependencies

/* mvn generate mvn install 下载所有必要的jars mvn test 测试应用程序制作... mvn site 构建站点下载依赖项

to deploy the site, we need to declare a location to distribute to in your pom.xml, similar to the repository for deployment. ... website scp://www.mycompany.com/www/docs/project/ ...

要部署站点,我们需要在您的 pom.xml 中声明一个要分发到的位置,类似于用于部署的存储库。... 网站 scp://www.mycompany.com/www/docs/project/ ...

mvn site-deploy deploys the site

mvn site-deploy 部署站点

how to build structure of site : The site.xml file is used to describe the layout of the site, and replaces the navigation.xml file used in Maven

如何构建站点结构:site.xml文件用于描述站点的布局,替代Maven中使用的navigation.xml文件

A sample is given below:

下面给出了一个示例:

Maven http://maven.apache.org/images/apache-maven-project.png http://maven.apache.org/ http://maven.apache.org/images/maven-small.gif

Maven http://maven.apache.org/images/apache-maven-project.png http://maven.apache.org/ http://maven.apache.org/images/maven-small.gif

<menu name="Maven 2.0">
  <item name="Introduction" href="index.html"/>
  <item name="Download" href="download.html"/>
  <item name="Release Notes" href="release-notes.html" />
  <item name="General Information" href="about.html"/>
  <item name="For Maven 1.x Users" href="maven1.html"/>
  <item name="Road Map" href="roadmap.html" />
</menu>

<menu ref="reports"/>

...

so in effect, we need to link our html to this structure format to make the website layout also in order for us to add any new css or such stuff, all we need to do is to put them into the resources part of the src folder

所以实际上,我们需要将我们的 html 链接到这种结构格式来制作网站布局,以便我们添加任何新的 css 或类似的东西,我们需要做的就是将它们放入 src 文件夹的资源部分

then we can create a war file of our project and lay it out in the httpd folder of apache or such similar folder ofour web server

然后我们可以为我们的项目创建一个war文件并将其放置在apache的httpd文件夹或我们的Web服务器的类似文件夹中

In case we need to generate projects, we need to add a few lines of code to our pom.xml file and that is: ... org.apache.maven.plugins maven-project-info-reports-plugin 2.0.1 ...

如果我们需要生成项目,我们需要在 pom.xml 文件中添加几行代码,即: ... org.apache.maven.plugins maven-project-info-reports-plugin 2.0.1 。 ..

also, site descriptors are to be set in site.xml

此外,站点描述符将在 site.xml 中设置

the details can be seen in the documentation of maven

细节可以在maven的文档中看到

maven structure with their importance:

Maven 结构及其重要性:

project/ pom.xml - Defines the project src/ main/ java/ - Contains all java code that will go in your final artifact.
See maven-compiler-plugin for details scala/ - Contains all scala code that will go in your final artifact. ////not needed for our current project as of yet See maven-scala-plugin for details resources/ - Contains all static files that should be available on the classpath in the final artifact. See maven-resources-plugin for details webapp/ - Contains all content for a web application (jsps, css, images, etc.)
See maven-war-plugin for details site/ - Contains all apt or xdoc files used to create a project website.
See maven-site-plugin for details
test/ java/ - Contains all java code used for testing.
See maven-compiler-plugin for details scala/ - Contains all scala code used for testing.
See maven-scala-plugin for details resources/ - Contains all static content that should be available on the classpath during testing. See maven-resources-plugin for details

project/ pom.xml - 定义项目 src/ main/ java/ - 包含将进入最终工件的所有 Java 代码。
有关详细信息,请参阅 maven-compiler-plugin scala/ - 包含将进入最终工件的所有 Scala 代码。////我们当前的项目还不需要,请参阅 maven-scala-plugin 以获取详细信息资源/ - 包含应在最终工件的类路径上可用的所有静态文件。有关详细信息,请参阅 maven-resources-plugin webapp/ - 包含 Web 应用程序的所有内容(jsps、css、图像等)。
请参阅 maven-war-plugin 以获取详细信息 site/ - 包含用于创建项目的所有 apt 或 xdoc 文件网站。
有关详细信息,请参阅 maven-site-plugin
test/ java/ - 包含用于测试的所有 java 代码。
有关详细信息,请参阅 maven-compiler-plugin scala/ - 包含用于测试的所有 Scala 代码。
有关详细信息,请参阅 maven-scala-plugin 资源/ - 包含测试期间应在类路径上可用的所有静态内容。有关详细信息,请参阅 maven-resources-plugin

mvn validate this will validate that all the dependencies are satisfied and nothing is missing mvn compile this will compile the project mvn verify checks whether the package is valid or not also in the project, the dependencies are to be inserted into the xml file

mvn validate 这将验证所有依赖项都得到满足并且没有遗漏任何东西 mvn compile 这将编译项目 mvn verify 检查项目中的包是否有效,依赖项将被插入到 xml 文件中

the example of dependencies injection is given below:: org.scala-lang scala-library 2.7.2-rc2 junit junit 3.8.1 test

依赖注入的例子如下: org.scala-lang scala-library 2.7.2-rc2 junit junit 3.8.1 test

Each dependency consists of several items:

每个依赖项由几个项目组成:

* groupId - The group of the dependency to rely on
* artifactId - The artifact in the group to rely on
* version - The version of the dependency to rely on
* scope - The "scope" of the dependency. Defaults to compile (more details later)
* packaging - The packaging for the dependency.  Defaults to jar (e.g. jar, war, ear)

You can integrate your static pages by following these steps:

您可以按照以下步骤集成静态页面:

* Put your static pages in the resources directory, ${basedir}/src/site/resources
* Create your site.xml and put it in ${basedir}/src/site
* Link to the static pages by modifying the menu section, create items and map them to the filenames of the static pages

mvn tomcat:deploy to deploy to tomcat or apache, you can go for this command

mvn tomcat:deploy 部署到 tomcat 或 apache,你可以使用这个命令

回答by Jesper

Free books about Mavencan be downloaded from Sonatype (where the original developers of Maven come from).

关于 Maven 的免费书籍可以从 Sonatype(Maven 的原始开发者来自那里)下载。

Also see the documentation on the Apache Mavenwebsite.

另请参阅Apache Maven网站上的文档。

回答by stacker

Where does Maven come into picture? I have used Ant and I understand Ants benefit of a standardized build process. But why do we need an advanced Ant in form of Maven?

Maven 在哪里出现?我使用过 Ant 并且我了解 Ants 的标准化构建过程的好处。但是为什么我们需要 Maven 形式的高级 Ant 呢?

  • Maven introduced "convention over configuration" this helps if some colleagues write bigger ant scipts than code. plus dependency management, the only trouble is to convert monolithic projects with many artifacts.
  • Maven 引入了“约定优于配置”,这对于某些同事编写比代码更大的蚂蚁脚本很有帮助。加上依赖管理,唯一的麻烦是转换具有许多工件的单体项目。

In any case, I need to use it, so where do I get started - basic flow, some good tutorials?

无论如何,我需要使用它,那么我从哪里开始 - 基本流程,一些好的教程?

helpful.

有帮助。

回答by Alfred

The latest netbeans also has a pretty good maven integration.

最新的 netbeans 也有很好的 maven 集成。

回答by Jason

If you are within an organization, try to build a maven repository proxy. Artifactoryis a good option.

如果您在组织内,请尝试构建 Maven 存储库代理。Artifactory是一个不错的选择。