简体   繁体   English

使用动态依赖项构建多个Java项目

[英]Build multiple java projects with dynamic dependencies

I have multiple java projects in a folder. 我在一个文件夹中有多个java项目。 Also there is a second folder with libraries, that might be used as build dependencies from the projects. 还有一个带库的第二个文件夹,可以用作项目的构建依赖项。 The projects may also have dependencies to other Projects. 项目也可能与其他项目有依赖关系。 What's the best approach to build all projects ? 构建所有项目的最佳方法是什么?

In other words I want to build the projects without explicit telling their dependencies.I think the biggest problem is the dependecy between the projects. 换句话说,我想在没有明确告知其依赖关系的情况下构建项目。我认为最大的问题是项目之间的依赖性。

There are multiple build systems that are available that you may use. 您可以使用多个可用的构建系统。 Maven has a complete dependency system built into it. Maven内置了完整的依赖系统。 Almost all third party open source jars are directly accessible via the World Wide Maven repository system. 几乎所有第三方开源jar都可以通过World Wide Maven存储库系统直接访问。 Basically, you describe the jar you need (groupId, artifactId, and version) and Maven will automatically fetch it for you. 基本上,您描述了您需要的jar(groupId,artifactId和version),Maven会自动为您提取它。 Not only that, but Maven also will build your project without having to create a build file. 不仅如此,Maven还将构建您的项目,而无需创建构建文件。 Instead, you have to describe your project in a project object model (a pom.xml file) and Maven will download everything you need, including all compilers, etc. 相反,您必须在项目对象模型pom.xml文件)中描述您的项目,Maven将下载您需要的所有内容,包括所有编译器等。

Almost all new projects use Maven, but Maven has a few downsides: 几乎所有新项目都使用Maven,但Maven有一些缺点:

  • Since you don't control a build process, it can sometimes feel like poking a prodding a black box to get the build to work the way you want. 由于您无法控制构建过程,因此有时候会感觉像是在刺戳黑色框以使构建按您希望的方式工作。
  • Documentation can be scant -- especially if you're moving beyond basic Java compiles. 文档可能很少 - 特别是如果您超越了基本的Java编译。
  • You usually have to arrange your project in a specific layout. 您通常必须以特定布局排列项目。 For example, source files should go under src/main/java while JUnit tests are under src/test/java . 例如,源文件应该在src/main/java而JUnit测试在src/test/java You don't have to follow the recommended layout, but then you'd have to modify the pom.xml file this way and that to get your build to work. 您不必遵循建议的布局,但是您必须以这种方式修改pom.xml文件,以使您的构建工作。 That defeats the whole purpose of the pom.xml in the first place. 这首先打败了pom.xml的整个目的。
  • If you already have another build system setup (like Ant), you lose everything. 如果您已经有另一个构建系统设置(如Ant),则会丢失所有内容。 There's no easy way to move from Ant to Maven. 从Ant到Maven没有简单的方法。

The other is called Ant with Ivy . 另一个叫常春藤蚂蚁。 Ivy uses Ant for building, but can access Maven's world wide repository system for third party dependencies. Ivy使用Ant进行构建,但可以访问Maven的全球存储库系统以获取第三方依赖项。 It's a great compromise if you already are heavily invested in Ant. 如果你已经在Ant上投入了大量资金,这是一个很好的妥协。 I also find Ant with Ivy to be better documented than Maven (although that's not too difficult). 我还发现使用常春藤的Ant比Maven更好(尽管这并不太难)。 There's an excellent chapter going over the basics of Ivy in Manning Publication's Ant in Action . 在曼宁出版的“行动中蚂蚁”中,有一篇很好的章节介绍了常春藤的基础知识。

With either process, I would recommend that you build a company wide Maven repository using either Nexus or Artifactory . 无论使用哪种流程,我都建议您使用NexusArtifactory构建公司范围的Maven存储库。 This way, any proprietary third party jars (like Oracle jars) can also be stored in your company wide Maven repository since they won't be in the standard World Wide Maven repository. 这样,任何专有的第三方jar(如Oracle jar)也可以存储在公司范围的Maven存储库中,因为它们不在标准的World Wide Maven存储库中。

By the way, if this is a company wide effort, and you are moving multiple Ant projects into Ivy, I have an Ivy project I use in Github that makes things easier. 顺便说一句,如果这是一个公司的努力,并且你正在将多个Ant项目移动到Ivy中,我有一个我在Github中使用的常春藤项目,这使得事情变得更容易。

Oh, there's a third possibility called Gradle which I know nothing about. 哦,有第三种可能叫做Gradle ,我对此一无所知。 I also believe it can use the World Wide Maven repository. 我也相信它可以使用World Wide Maven存储库。 It's based on Groovy which is based on Java syntax, and that's about all I can say. 它基于Groovy,它基于Java语法,这就是我所能说的。 Maybe others can fill you in on the details. 也许其他人可以填写您的详细信息。 The Gradle group contends it solves a lot of problems of both Ant/Ivy and Maven. Gradle小组认为它解决了Ant / Ivy和Maven的许多问题。

Whatever tool you use, if you have various projects interdependent, you need to be clear on the independent ones which will be built first before building the dependent projects. 无论您使用什么工具,如果您有各种相互依赖的项目,您需要明确在构建依赖项目之前将首先构建的独立项目。 You need to have a clear dependency structure for your projects. 您需要为项目建立清晰的依赖关系结构。

You can do this with Apache Ivy . 你可以用Apache Ivy做到这一点。 You can lay out the locations for you common libraries, define published artifacts and inter-dependencies in an ivy.xml document in each project, and let a top-level Ant build with the Ivy tasks figure out what the build order should be based on those dependencies. 您可以为每个项目中的ivy.xml文档布置公共库的位置,定义已发布的工件和相互依赖关系,并让使用常春藤任务的顶级Ant构建根据构建顺序确定构建顺序那些依赖。

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

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