Maven: Selecting Parent Project Based On Profile

試著忘記壹切 提交于 2020-02-22 07:41:09

问题


I have a maven project - it is a plugin for jenkins. It's parent should be a:

<parent>
  <groupId>org.jenkins-ci.plugins</groupId>
  <artifactId>plugin</artifactId>
  <version>1.414</version>
</parent>

But at the same time this plugin can be also used for hudson, without changing any line of code. But the parent project for it should be:

<parent>
  <groupId>org.jvnet.hudson.plugins</groupId>
  <artifactId>hudson-plugin-parent</artifactId>
  <version>2.0.1</version>
</parent>

Can I specify 2 different profiles for that and use them to build plugin for jenkins or hudson accordingly? So that I call something like that:

mvn package -P jenkins

or

mvn package -P hudson

I have tried to specify properties in profiles, but those are not replaced by their values inside the <parent> tag. So is there any other possibility to build plugin for both, but with as much as possible common code and files?

Added: So, if I cannot do that, what should I do then? How to refactor? What the new structure should be?


回答1:


As already mentioned, this is not possible. Also, it is not possible to set a property for the parent's version as the interpolation for that happens a lot earlier than the handling of the profiles.

I would suggest that you create a masterbuild project as follows:

master
|-plugin-jenkins
|-plugin-hudson
|-plugin-assembly

The master should build all three as usual. However, in the assembly, you could add each of the two plugins as dependencies in separate profiles. And... each of these plugins can have the parent you like.

This is obviously somewhat a deviation from the Maven convention, but I believe it is a solution to your problem.




回答2:


It's not possible because the tag "parent" is not available in the profiles section of the pom.




回答3:


Currently we decided to stick with 1 repository and 2 separate pom.xml files, giving maven key which pom.xml use to build the project.

mvn package -f pom-jenkins.xml
mvn package -f pom-hudson.xml



回答4:


No you cannot do that. you will have to refactor somehow to avoid the necessity.




回答5:


As mentioned already not possible. I would suggest to make separate projects for jenkins plugin and hudson plugin. I assume that in not that far future that will not work anymore cause Hudons and Jenkins will diverge.




回答6:


In general, you should be able to set the {group,artifact}Id and version of the parent POM via Java System Properties or Environment Variables, but it seems there is a Bug in Maven which will only be fixed in 4.x: https://issues.apache.org/jira/browse/MNG-624

Another solution is to delegate the inclusion of the parent POM to your own parent POMs which you reference in the relativePath element, and change the content of the target e.g. via a symlink or cp command.

So in the main POM you would write:

<parent>
    <groupId>org.mycompany.project</groupId>
    <artifactId>foo-artifact</artifactId>
    <version>1.0.0</version>
    <relativePath>./my-parent.pom</relativePath>
</parent>

And in my-parent-jenkins you would just put:

<groupId>org.mycompany.project</groupId>
<artifactId>foo-artifact</artifactId>
<version>1.0.0</version>

<parent>
    <groupId>org.jenkins-ci.plugins</groupId>
    <artifactId>plugin</artifactId>
    <version>1.414</version>
</parent>

The same project information with the block for hudson you put in my-parent-hudson.pom.

No you can either use

ln -s my-parent-jenkins.pom my-parent.pom

or

ln -s my-parent-hudson.pom  my-parent.pom

to include the respective parent POM without the need to maintain two different main POM files for your project.

In case POM does not exist at the place referenced in relativePath, Maven will look up the POM in the remote repository[1], which is also an easy way to overwrite a parent POM locally.

[1] http://maven.apache.org/components/ref/3.3.9/maven-model/maven.html#class_parent



来源:https://stackoverflow.com/questions/6507019/maven-selecting-parent-project-based-on-profile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!