Maven best practice for generating artifacts for m

2019-01-22 14:04发布

I have a project that need to be deployed into multiple environments (prod, test, dev). The main differences mainly consist in configuration properties/files.

My idea was to use profiles and overlays to copy/configure the specialized output. But I'm stuck into if I have to generate multiple artifacts with specialized classifiers (ex: "my-app-1.0-prod.zip/jar", "my-app-1.0-dev.zip/jar") or should I create multiple projects, one project for every environment ?!
Should I use maven-assembly-plugin to generate multiple artifacts for every environment ? Anyway, I'll need to generate all them at once so it seams that the profiles does not fit ... still puzzled :(

Any hints/examples/links will be more than welcomed.

As a side issue, I'm also wondering how to achieve this in a CI Hudson/Bamboo to generate and deploy these generated artifacts for all the environments, to their proper servers (ex: using SCP Hudson plugin) ?

6条回答
Explosion°爆炸
2楼-- · 2019-01-22 14:08

I had this exact scenario last summer.

I ended up using profiles for each higher environment with classifiers. Default profile was "do no harm" development build. I had a DEV, INT, UAT, QA, and PROD profile.

I ended up defining multiple jobs within Hudson to generate the region specific artifacts.

The one thing I would have done differently was to architect the projects a bit differently so that the region specific build was outside of the modularized main project. That was it would simply pull in the lastest artifacts for each specific build rather than rebuild the entire project for each region.

In fact, when I setup the jobs, the QA and PROD jobs were always setup to build off of a tag. Clearly this is something that you would tailor to your specific workplace rules on deployment.

查看更多
该账号已被封号
3楼-- · 2019-01-22 14:14

We implemented a m2 plugin to build the final .properties using the following approach:

  • The common, environment-unaware settings are read from common.properties.
  • The specific, environment-aware settings are read from dev.properties, test.properties or production.properties, thus overriding default values if necessary.
  • The final .properties files is written to disk with the Properties instance after reading the files in given order.
  • Such .properties file is what gets bundled depending on the target environment.
查看更多
再贱就再见
4楼-- · 2019-01-22 14:24

I prefer to package configuration files separately from the application. This allows you to run the EXACT same application and supply the configuration at run time. It also allows you to generate configuration files after the fact for an environment you didn't know you would need at build time. e.g. CERT I use the "assembly" tool to zip up each domain's config files into named files.

查看更多
淡お忘
5楼-- · 2019-01-22 14:27

I would use the version element (like 1.0-SNAPSHOT, 1.0-UAT, 1.0-PROD) and thus tags/branches at the VCS level in combination with profiles (for environments specific things like machines names, user name passwords, etc), to build the various artifacts.

查看更多
Viruses.
6楼-- · 2019-01-22 14:30

We use profiles to achieve that, but we only have the default profile - which we call "development" profile, and has configuration files on it, and we have a "release" profile, where we don't include the configuration files (so they can be properly configured when the application is installed).

I would use profiles to do it, and I would append the profile in the artifact name if you need to deploy it. I think it is somewhat similar to what Pascal had suggested, only that you will be using profiles and not versions.

PS: Another reason why we have dev/ release profiles only, is that whenever we send something for UAT or PROD, it has been released, so if there is a bug we can track down what the state of the code was when the application was released - it is easier to tag it in SVN than trying to find its state from the commit history.

查看更多
啃猪蹄的小仙女
7楼-- · 2019-01-22 14:30

Try using https://github.com/khmarbaise/multienv-maven-plugin to create one main WAR and one configuration JAR for each environment.

查看更多
登录 后发表回答