简体   繁体   English

使用Maven发布不同的配置

[英]Release different configurations with Maven

I'm currently migrating our build process to Maven from Ant. 我目前正在将构建过程从Ant迁移到Maven。 Our application is deployed to many different customers, each with a unique set of dependencies and and configuration. 我们的应用程序已部署到许多不同的客户,每个客户都有一组独特的依赖项和配置。 I can implement different profiles to model these and build the required wars from them. 我可以实现不同的配置文件以对它们进行建模,并根据它们建立所需的战争。 However this is a process that happens at compile time. 但是,这是一个在编译时发生的过程。

Each release is tagged under SVN as well as uploaded to our internal Nexus repository. 每个版本都标记为SVN,并已上传到我们的内部Nexus存储库。 I want to be able to take a defined release and reconstruct it based a profile. 我希望能够采用定义的发行版并根据配置文件对其进行重构。 Is there a way to do something like this? 有没有办法做这样的事情? Is there something other than profiles I should be using? 除了个人资料外,我还应该使用其他东西吗?

"declare several execution for the war plugin to produce several artifacts (and install/deploy them)" This sounds like this might be the way forward. “声明war插件的多次执行以产生多个工件(并安装/部署它们)”听起来这可能是前进的方向。 How would I go about doing this? 我将如何去做呢?

This goes a bit against a Maven golden rule (the one main artifact per module rule) but can be done. 这有点违反Maven黄金法则(每个模块一个主要工件),但是可以做到。 The One artifact with multiple configurations in Maven blog post describes one way to implement this approach: Maven博客文章中的具有多个配置一个工件描述了一种实现此方法的方法:

I decided to put all the environment specific configuration in a special source tree, with the following structure: 我决定将所有特定于环境的配置放在具有以下结构的特殊源代码树中:

 +-src/ +-env/ +-dev/ +-test/ +-prod/ 

Then I configured the maven-war-plugin to have three different executions (the default plus two extra), one for each environment, producing three different war files: beer-1.0-dev.war, beer-1.0-test.war and beer-1.0-prod.war. 然后,我将maven-war-plugin配置为具有三个不同的执行(默认加上两个额外的执行),每个环境一个,产生三个不同的war文件:beer-1.0-dev.war,beer-1.0-test.war和beer -1.0-prod.war。 Each of these configurations used the standard output files from the project and then copied the content from the corresponding src/env/ directory on to the output files, enabling an override file to be placed in the corresponding src/env/ directory. 这些配置中的每一个都使用项目中的标准输出文件,然后将内容从相应的src/env/目录复制到输出文件中,从而可以将替代文件放置在相应的src/env/目录中。 It also supported copying a full tree structure into the output directory. 它还支持将完整的树结构复制到输出目录中。 Thus if you for instance wanted to replace the web.xml in test you simply created the following directory: 因此,例如,如果您想在测试中替换web.xml ,则只需创建以下目录:

 src/env/test/WEB-INF/ 

and placed your test specific web.xml in this directory and if you wanted to override a db.property file placed in the classpath root directory for the test environment you created the following directory: 并将特定于测试的web.xml放置在此目录中,如果要覆盖放置在测试环境的类路径根目录中的db.property文件,请创建以下目录:

 src/env/test/WEB-INF/classes 

and placed your test specific db.property file in this directory. 并将测试专用的db.property文件放置在此目录中。

I kept the src/main directory configured for development environment. 我为开发环境保留了src/main目录。 The reason for this was to be able to use the maven-jetty-plugin without any extra configuration. 这样做的原因是无需任何额外配置即可使用maven-jetty-plugin。 Configuration 组态

Below you find the maven-war-plugin configuration that I used to accomplish this: 在下面,您找到我用来完成此任务的maven-war-plugin配置:

 <plugin> <artifactId>maven-war-plugin</artifactId> <configuration> <classifier>prod</classifier> <webappDirectory>${project.build.directory}/${project.build.finalName}-prod</webappDirectory> <webResources> <resource> <directory>src/env/prod</directory> </resource> </webResources> </configuration> <executions> <execution> <id>package-test</id> <phase>package</phase> <configuration> <classifier>test</classifier> <webappDirectory>${project.build.directory}/${project.build.finalName}-test</webappDirectory> <webResources> <resource> <directory>src/env/test</directory> </resource> </webResources> </configuration> <goals> <goal>war</goal> </goals> </execution> <execution> <id>package-dev</id> <phase>package</phase> <configuration> <classifier>dev</classifier> <webappDirectory>${project.build.directory}/${project.build.finalName}-dev</webappDirectory> <webResources> <resource> <directory>src/env/dev</directory> </resource> </webResources> </configuration> <goals> <goal>war</goal> </goals> </execution> </executions> </plugin> 

(...) I can define each customer project with profiles but I don't know if there's a way to release them to a repository. (...)我可以使用配置文件定义每个客户项目,但是我不知道是否有将它们发布到资源库的方法。

You have several options: 您有几种选择:

  • use profiles and run the build several times (create artifacts with a classifier and install/deploy them) 使用配置文件并运行构建几次(使用分类器创建工件并安装/部署它们)
  • declare several execution for the war plugin to produce several artifacts (and install/deploy them) 声明war插件的多个执行以产生多个工件(并安装/部署它们)
  • use different modules (and maybe war overlays to merge a common part with a specific one) 使用不同的模块(可能是战争叠加层 ,将一个公共部分与一个特定部分合并)

Or at least a way in Maven to automatically build an artifact with a specified profile from say an SVN tag. 或者至少是Maven中一种通过SVN标签使用指定配置文件自动构建工件的方法。

Well, this is doable. 好吧,这是可行的。 But without more details about a particular problem, it's hard to be more precise. 但是,如果没有有关特定问题的更多详细信息,就很难更加精确。

I would take a look at your architecture and see if there is a way to split up your project into multiple projects. 我将看一下您的体系结构,看看是否有一种方法可以将您的项目拆分为多个项目。 One would be the main code base. 一个将是主要的代码库。 The other projects would depend on the JAR file produced by the main project and add in their own configuration, dependencies, etc to produce your final artifact. 其他项目将依赖于主项目生成的JAR文件,并添加其自己的配置,依赖项等来生成最终的工件。

This would let you version customer specific code independently of each other as well as keeping common code in one place and separate from customer specific stuff. 这样一来,您就可以彼此独立地对客户特定的代码进行版本控制,也可以将通用代码放在一个地方,并与客户特定的内容分开。

I ended up doing something slightly different. 我最终做了些不同的事情。 We're not storing the releases in our internal repository. 我们不会在内部存储库中存储版本。 Instead we're building using Hudson and a multi-configuration project (one configuration/profile for each customer). 相反,我们使用Hudson和一个多配置项目(每个客户一个配置/配置文件)进行构建。 This way when a release is made the Hudson job is run to build different wars for all customers. 这样,在发布版本时,将运行Hudson作业以为所有客户建立不同的战争。 They are then stored on the Hudson server instead of Nexus. 然后将它们存储在Hudson服务器上,而不是Nexus上。 Builds for specific versions and customers can also be built at any time from the releases in Nexus. 也可以随时从Nexus中的版本构建针对特定版本和客户的构建。 – samblake Mar 16 '11 at 12:32 – samblake 2011年3月16日12:32

Have you taken a look at the Maven Assembly plugin ? 您是否看过Maven Assembly插件

This plugin allows you to customize how your distribution is assembled - ie what format ( .tar.gz , .zip , etc), directory structure, etc. I think you should be able to bind several instances of the plugin to the package phase to assemble multiple variations of your output (ie the packaging for customer 1, customer2, etc, separately). 此插件可让您自定义发行版本的组装方式,即格式( .tar.gz.zip等),目录结构等。我认为您应该能够将插件的多个实例绑定到package阶段组装输出的多个变体(即分别为客户1,客户2等包装)。

The deploy plugin should then automatically handle deploying each of your assembled packages in the target directory to the repository. 然后,deploy插件应自动处理将target目录中的每个组装好的程序包部署到存储库。

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

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