maven: generating several “artifacts” with a same pom file?

后端 未结 4 1933
栀梦
栀梦 2021-01-04 11:30

I have a project here which, currently, uses one pom.xml (link) to generate one \"artifact\".

What I\'d like to do is split the project, let\'s call it p, into:

相关标签:
4条回答
  • 2021-01-04 11:44

    You could use the maven-assembly plugin. If the two artifacts have overlapping classes and updating one always requires to update the other one, then this is a good choice.

    You have to think life cycle of the artifacts. Do both artifacts need to have the same version number? Do you want to publish a new version of p-core whenever you fix a bug in p-format? Is p-core useful on its own?

    The assembly plugin should primarily be used to provide a different packaging for the same artifact, I guess.

    0 讨论(0)
  • 2021-01-04 11:49

    Your life will be much easier if you simply break out p-format into a separate project that depends on p-core.

    Although there are ways to publish multiple artifacts per project, Maven is designed around one artifact per pom/module (plus test artifacts). From experience, clever attempts to work around this design point will end up biting you eventually - usually in the form of an issue 6 months down the road where packaging won't work properly on someone's machine.

    The scenario you describe fits well within the multi-project use case, and I'd suggest you go with the flow and make your future self happy.

    0 讨论(0)
  • 2021-01-04 11:52

    I had a similar case and I agree with Michal's suggestion. However, I would like to extend the proposed solution further:

    I'm assuming p has a single source and both p-core and p-format are generated from it. So you should create a parent POM for p with children modules (with their respective POMs) -- one for 'core' and one for 'format' but have these both source from the parent's /src/.

    So essentially we segregating the build processes for 'core' and 'format' to generate specific artifacts but not replicating the original sources for p.

    Maven supports but does not recommended this approach but I don't see why they shouldn't. In my opinion, its structured, clean and concern-seperating without any long-term implications and there is no better alternative.

    http://maven.apache.org/guides/mini/guide-using-one-source-directory.html

    0 讨论(0)
  • 2021-01-04 11:58

    You should create two separated projects (two separated POMs), but probably a good idea would be to create common parent for them that also aggregate them as modules. Look at Project Inheritance and Project Aggregation sections of Introduction to the POM for some basics.

    In Maven, it's never a good idea to try to hack it so it produces many different artifacts from one POM.

    0 讨论(0)
提交回复
热议问题