简体   繁体   English

目标文件夹vs本地存储库

[英]target folder vs local repository

I know that Maven houses the outcome of the build in the local repository (artifacts goes installed under ~/.m2/repository/ ), but it also outputs the compiled classes in the target folder next to src . 我知道Maven包含了本地存储库中构建的结果(工件安装在~/.m2/repository/ ),但它也将编译后的类输出到src旁边的target文件夹中。

Is there any difference between what goes in the local repository and what goes in the target folder? 本地存储库中的内容与target文件夹中的内容之间有什么区别吗?

They are completely different and shouldn't be mixed up. 它们是完全不同的,不应混淆。

  • target represents the build directory. target表示构建目录。 This is to say, every temporary file that is generated during the build from the sources ends up there. 也就是说,从源代码构建期间生成的每个临时文件都会在那里结束。 Quite notably, you'll find the compiled classes of the main and test Java sources, but you'll also find lots of things in there (generated source files, filtered files, etc.). 非常值得注意的是,您将找到主要和测试Java源代码的编译类,但您也会发现很多内容 (生成的源文件,过滤的文件等)。 What matters, is that everything that is contained in this folder is inherently temporary. 重要的是,此文件夹中包含的所有内容本质上都是暂时的。 You can delete it at any time, running mvn clean , and be assured that the next build will (or at least should ) work just fine. 您可以随时删除它,运行mvn clean ,并确保下一个构建将(或至少应该 )正常工作。 All the files and folders generated under target serve a single purpose: create the artifacts of the project. target下生成的所有文件和文件夹都有一个目的:创建项目的工件。 A Maven project, for example with jar packaging, will have a single main artifact, which is composed of its final name with a jar extension, and will contain the compiled Java classes. Maven项目,例如jar包装,将有一个主要工件,它由带有jar扩展名的最终名称组成,并将包含已编译的Java类。 The final name can be a custom name, set within the POM, or the default one derived from the Maven coordinates of the project. 最终名称可以是自定义名称,在POM中设置,或者是从项目的Maven坐标派生的默认名称。 Such a project can also have additional attached artifacts, like a test JAR, or a sources JAR. 此类项目还可以具有其他附加工件,例如测试JAR或源JAR。

  • The local repository only contains the artifacts. 本地存储库仅包含工件。 There are no temporary files in there. 那里没有临时文件。 What is installed when running mvn install is strictly the generated artifacts of the Maven project, ie the end products, plus the POM file of the project. 运行mvn installmvn install是严格生成的Maven项目工件,即最终产品,以及项目的POM文件。 Everything that served to create them isn't put in the local repository, and the build of a project must never put temporary things in there. 用于创建它们的所有东西都不会放在本地存储库中,并且项目的构建绝不能将临时内容放在那里。 Keep in mind that the local repository is a Maven repository, and, as such, follows a strict naming scheme: a project with a group id of my.groupid , an artifact id of my-artifactid and a version of 1.0 will get installed in the folder my/groupid/my-artifactid/1.0 ; 请记住,本地存储库是一个Maven存储库,因此遵循严格的命名方案:组ID为my.groupid的项目,工件ID为my-artifactid ,版本为1.0将安装在文件夹my/groupid/my-artifactid/1.0 ; in which you'll find the POM file, and all the other artifacts. 在其中您将找到POM文件以及所有其他工件。 The name of the artifacts themselves cannot be overriden: it will be my-artifactid-1.0.jar for a JAR project (perhaps with a classifier added). 工件本身的名称不能被覆盖:对于JAR项目(可能添加了分类器 ),它将是my-artifactid-1.0.jar

This is generally a source of confusion: the name of the main artifact file that is generated under the target folder is completely distinct from the name that it will have in the local repository when installed, or in remote repositories when deployed. 这通常是一个混淆的来源:在target文件夹下生成的主工件文件的名称与安装时在本地存储库中具有的名称完全不同,或者在部署时在远程存储库中。 The first can be controlled, but the latter is defined by the naming scheme of the repository, which is calculated from the coordinates. 第一个可以被控制,但后者由存储库的命名方案定义,该方案是从坐标计算的。

To recap: target contains all the gory temporary details during the build which creates the artifacts of a project (main JAR, sources, Javadoc... ie everything that is supposed to be deployed and released by that project), while the local repository (and remote repositories) will contain only the artifacts themselves. 回顾一下: target包含构建期间的所有血腥临​​时详细信息,这些详细信息创建项目的工件(主JAR,源代码,Javadoc ......即应该由该项目部署和发布的所有内容),而本地存储库(和远程存储库)将仅包含工件本身。

Not much in terms of the generated module.jar if that's what you are really concern about. 就生成的module.jar如果这是你真正关心的那么多。 The .jar generated is the same, also considering recompiling the code would clean your /target folder but not the .m2 one. 生成的.jar是相同的,也考虑重新编译代码将清理你的/target文件夹而不是.m2文件夹。

Though /target folder would generally be composed of the compiled source classes /target/classes and /target/generated-source etc along with a module.jar . 虽然/target文件夹通常由已编译的源类/target/classes/target/generated-source等以及module.jar

On the other hand the local ~.m2/repository would consist of module.jar along with the pom.xml for that module and all the configs( repositories , dependencies etc) to rebuild that module from if required. 另一方面,本地module.jar ~.m2/repository将包含module.jar以及该模块的pom.xml以及从需要时重建该模块的所有配置( repositoriesdependencies等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM