Java “mvn clean package”和“mvn clean install”有什么不同?

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

How are "mvn clean package" and "mvn clean install" different?

javamaven

提问by Ben

What exactly are the differences between mvn clean packageand mvn clean install? When I run both of these commands, they both seem to do the same thing.

mvn clean package和之间究竟有什么区别mvn clean install?当我运行这两个命令时,它们似乎都在做同样的事情。

回答by Daniel Kaplan

Well, both will clean. That means they'll remove the target folder. The real question is what's the difference between package and install?

好吧,两者都会清洁。这意味着他们将删除目标文件夹。真正的问题是 package 和 install 之间有什么区别?

packagewill compile your code and also package it. For example, if your pom says the project is a jar, it will create a jar for you when you package it and put it somewhere in the target directory (by default).

package将编译您的代码并将其打包。例如,如果你的 pom 说该项目是一个 jar,它会在你打包时为你创建一个 jar 并将其放在目标目录中的某个位置(默认情况下)。

installwill compile and package, but it will also put the package in your local repository. This will make it so other projects can refer to it and grab it from your local repository.

install将编译和打包,但它也会将包放在您的本地存储库中。这将使其他项目可以引用它并从您的本地存储库中获取它。

Documentation

文档

回答by Aarish Ramesh

Package & install are various phases in maven build lifecycle. package phase will execute all phases prior to that & it will stop with packaging the project as a jar. Similarly install phase will execute all prior phases & finally install the project locally for other dependent projects.

打包和安装是 Maven 构建生命周期中的各个阶段。package 阶段将在此之前执行所有阶段,并将停止将项目打包为 jar。类似地,安装阶段将执行所有先前的阶段并最终为其他依赖项目在本地安装项目。

For understanding maven build lifecycle please go through the following link https://ayolajayamaha.blogspot.in/2014/05/difference-between-mvn-clean-install.html

要了解 maven 构建生命周期,请通过以下链接https://ayolajayamaha.blogspot.in/2014/05/difference-between-mvn-clean-install.html

回答by Abdul Gafoor

packagewill generate Jar/war as per POM file. installwill install generated jar file to the local repository for other dependencies if any.

将根据 POM 文件生成 Jar/war。 install会将生成的 jar 文件安装到本地存储库以用于其他依赖项(如果有)。

installphase comes after packagephase

安装阶段在打包阶段之后

回答by Nisarg Patil

packagewill add packaged jaror warto your targetfolder, We can check it when, we empty the target folder (using mvn clean) and then run mvn package.
installwill do all the things that packagedoes, additionally it will add packaged jaror warin local repository as well. We can confirm it by checking in your .m2folder.

package将打包jar或添加war到您的 target文件夹中,我们可以检查它何时,我们清空目标文件夹(使用 mvn clean)然后运行mvn package
install将做所有的事情package,此外,它还将添加打包jarwar在本地存储库中。我们可以通过检查您的.m2文件夹来确认。

回答by Ketan R

What clean does (common in both the commands) - removes all files generated by the previous build

clean 做什么(在两个命令中都很常见) - 删除先前构建生成的所有文件



Coming to the difference between the commands packageand install, you first need to understand the lifecycle of a maven project

谈到命令packageinstall之间的区别,您首先需要了解maven 项目生命周期



These are the default life cycle phases in maven

这些是 maven 中的默认生命周期阶段

  • validate- validate the project is correct and all necessary information is available
  • compile- compile the source code of the project
  • test- test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package- take the compiled code and package it in its distributable format, such as a JAR.
  • verify- run any checks on results of integration tests to ensure quality criteria are met
  • install- install the package into the local repository, for use as a dependency in other projects locally
  • deploy- done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.
  • 验证- 验证项目是否正确并且所有必要的信息都可用
  • compile- 编译项目的源代码
  • 测试- 使用合适的单元测试框架测试编译的源代码。这些测试不应该要求打包或部署代码
  • - 获取编译后的代码并将其打包为其可分发格式,例如 JAR。
  • 验证- 对集成测试的结果进行任何检查,以确保满足质量标准
  • install- 将包安装到本地存储库中,用作本地其他项目的依赖项
  • 部署- 在构建环境中完成,将最终包复制到远程存储库以与其他开发人员和项目共享。


How Maven works is, if you run a command for any of the lifecycle phases, it executes each default life cycle phase in order, before executing the command itself.

Maven 的工作原理是,如果您为任何生命周期阶段运行命令,它会在执行命令本身之前按顺序执行每个默认生命周期阶段。

order of execution

执行顺序

validate >> compile >> test (optional) >> package >> verify >> install >> deploy

验证>>编译>>测试(可选)>>包>>验证>>安装>>部署

So when you run the command mvn package, it runs the commands for all lifecycle phases till package

因此,当您运行命令mvn package 时,它会运行所有生命周期阶段的命令,直到 package

validate >> compile >> test (optional) >> package

验证 >> 编译 >> 测试(可选)>> 包

And as for mvn install, it runs the commands for all lifecycle phases till install, which includes package as well

至于mvn install,它运行所有生命周期阶段的命令,直到安装,其中也包括包

validate >> compile >> test (optional) >> package >> verify >> install

验证>>编译>>测试(可选)>>包>>验证>>安装



So, effectively what it means is, installcommands does everything that packagecommand does and some more (install the package into the local repository, for use as a dependency in other projects locally)

所以,实际上它的意思是,安装命令完成命令所做的一切以及更多(将包安装到本地存储库中,用作本地其他项目的依赖项)

Source: Maven lifecycle reference

来源:Maven 生命周期参考