How to use maven profile and overlays to build and package with specified project from multiple?

南楼画角 提交于 2019-12-25 02:25:51

问题


I have one web project called MyWebProject which having sub modules also and packaging as POM, I have other two simple java project called SimpleJavaProject1 and SimpleJavaProject2 which having packaging as JAR.

I am having dependency for both in web peoject. So I have to use Maven Profile and Overlays such way, that when I will build and package my web project with profile JavaProject1 then web project includes SimpleJavaProject1 in its war and when I said JavaProject2 then it should include SimpleJavaProject2. And it should use Overlays only for specified java project.

Can I use Overlays in Profile?

Please suggest some idea if any...


回答1:


I'm not familiar with overlays, but hopefully this approach will work for them too.

Typically one solves this sort of problem by defining a property in your parent POM, based on the profile:

<profiles>
    <profile>
        <id>JavaProject1</id>
        <properties>
          <java.project>SimpleJavaProject1</java.project>
          <java.project.version>1.1</java.project.version>
        </properties>
    </profile>
    <profile>
        <id>JavaProject2</id>
        <properties>
          <java.project>SimpleJavaProject2</java.project>
          <java.project.version>1.2</java.project.version>
        </properties>
    </profile>
</profiles>

Then use this property when you define your dependency (and hopefully your overlays too):

<dependency>
    <groupId>com.foo</groupId>
    <artifactId>${java.project}</artifactId>
    <version>${java.project.version}</version>
</dependency>



回答2:


Got it...referring @Duncan answer I have tried following and its worked. :-)

Following are my Profiles,

<profile>
    <id>JavaProject1</id>
    <properties>
        <roject.groupId>mygroupId</project.groupId>
        <roject.artifactId>myartifactId</project.artifactId>
        <roject.version>${myversion}</project.version>
    </properties>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
</profile>

<profile>
    <id>JavaProject2</id>
    <properties>
        <roject.groupId>mygroupId</project.groupId>
        <roject.artifactId>myartifactId</project.artifactId>
        <roject.version>${myversion}</project.version>
    </properties>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
</profile>

And I have added Overlays in war plugin as follows,

<overlays>
    <overlay>
        <groupId>${project.groupId}</groupId>
        <artifactId>${project.artifactId}</artifactId>
        <type>jar</type>
        <targetPath>WEB-INF/classes</targetPath>
    </overlay>
</overlays>

It worked successfully. :-)



来源:https://stackoverflow.com/questions/24385245/how-to-use-maven-profile-and-overlays-to-build-and-package-with-specified-projec

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