Maven Profile - 根据包装激活配置文件

发布于 2024-09-15 16:09:19 字数 181 浏览 10 评论 0原文

我有一个 POM,它声明了我的项目中常见的 Web 应用程序内容。我使用它作为所有 Web 应用程序的父级。

是否可以仅在包装战争时激活配置文件?我尝试过属性方法,但这不起作用(因为它不是系统/环境属性)。

由于构建失败,我可以在安装 POM 时简单地禁用该配置文件,但我希望它本身更加智能。

沃尔特

I have a POM which declares web application stuff that is common to my projects. I use this as the parent for all web applications.

Is it possible to activate a profile only when the packaging is war? I have tried the property approach, but that doesn't work (as it isn't a system/environment property).

Since this fails the build, I can simply disable that profile when installing the POM, but I'd like it to be more intelligent on its own.

Walter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

天冷不及心凉 2024-09-22 16:09:19

您可以简单地检查 src/main/webapp 是否存在。每个使用 Maven 标准目录布局的 Web 应用程序都应包含此文件夹。这样您就可以避免不必要的虚拟文件。

<profile>
    <id>custom-profile-eclipse-project-generation-webapp</id>
    <activation>
        <file>
            <exists>${basedir}/src/main/webapp</exists>
        </file>
    </activation>
    <build>
    </build>
</profile>

更准确地说,您还可以检查 ${basedir}/src/main/webapp/WEB-INF/web.xml 是否存在。这应该明确地确定了一个战争项目。

对于我自己来说,我在常见的 super-pom 中使用此配置来为不同的项目类型配置 maven-eclipse-plugin。在我们组织中的相同项目类型上获得同质的 eclipse 配置非常方便,特别是当开发人员直接在多模块项目上运行 eclipse:eclipse 时。

You can simply check the existence of src/main/webapp. Each web application that uses the Maven standard directory layout should contain this folder. So you avoid unnecessary dummy files.

<profile>
    <id>custom-profile-eclipse-project-generation-webapp</id>
    <activation>
        <file>
            <exists>${basedir}/src/main/webapp</exists>
        </file>
    </activation>
    <build>
    </build>
</profile>

More precise you can also check for the the existence of ${basedir}/src/main/webapp/WEB-INF/web.xml. That should definitively identify a war-project.

For myself I use this configuration in my common super-pom to configure the maven-eclipse-plugin for different project types. Thats very handy to get homogenous eclipse-configurations over the same project type in our organization, especially when developers straightforwardly run eclipse:eclipse on multi-module-projects.

铜锣湾横着走 2024-09-22 16:09:19

我知道这并不能直接回答您的问题,但解决此类问题的通常解决方法是仅使用专业化(与类一样)。

这样您就拥有了具有所有常见行为的 MasterPom。

MasterWarPom 扩展了 MasterPom(是它的父级),并将任何“打包就是战争”专业化放在这里。

同样,您可以使用 MasterJarPom 等......

这样可以很好地区分差异。

I know this isn't answering your question directly, but the usual workaround for problems like this is to just use specialization (as with classes).

So you have your MasterPom with all common behavior.

MasterWarPom that extends MasterPom (is it's parent), and put any 'packing is war' specializations in here.

Likewise you could have MasterJarPom, etc ...

That way the differences are split out nicely.

想挽留 2024-09-22 16:09:19

没有干净的方法可以做到这一点,父模块无法知道子模块的包装。 (非干净的解决方案将涉及创建一个解析子模块的 pom 等的插件。)

There's no clean way to do that, the parent module has no way of knowing the child's packaging. (Non-clean solutions would involve creating a plugin that parses the child module's pom etc.)

回忆凄美了谁 2024-09-22 16:09:19

对于这些场景,我能想到的最好的方法是使用基于文件的激活触发器。
例如,我的父 pom

<profile>
   <id>maven-war-project</id>
   <activation>
     <file><!-- add a file named .maven-war-project-marker to webapp projects to activate this profile -->
       <exists>${basedir}/.maven-war-project-marker</exists>
     </file>
   </activation>
   <build>
     <plugins>
   <!-- configuration for webapp plugins here  -->
     </plugins>
   </build>

和 webapp 项目继承自该父项,其中包含一个名为
'.maven-war-project-marker'
激活配置文件

这看起来很迟钝,但工作相当可靠,而
- 如果由不同的人或系统进行构建,则使用属性激活是不可靠的,
- 从特定于类型的父级继承对我来说有点麻烦,因为祖父母-pom 相对频繁地更改版本,因为它用于定义公共依赖项的“标准”或首选版本,而这又需要所有特定于类型的相应版本父母除了祖父母版本外没有任何变化

The best I've been able to come up with for these sorts scenarios has been to use a file-based activation trigger.
eg my parent pom has

<profile>
   <id>maven-war-project</id>
   <activation>
     <file><!-- add a file named .maven-war-project-marker to webapp projects to activate this profile -->
       <exists>${basedir}/.maven-war-project-marker</exists>
     </file>
   </activation>
   <build>
     <plugins>
   <!-- configuration for webapp plugins here  -->
     </plugins>
   </build>

and webapp projects that inherit from this parent contain a file named
'.maven-war-project-marker'
that activates the profile

This looks pretty obtuse but works fairly reliably whereas
- using property-activation is unreliable if a different person or system does the build,
- inheriting from type-specific parents became a bit cumbersome for me as the grandparent-pom changes version relatively frequently as it is used to define 'standard' or preferred versions of common dependencies which in turn required corresponding releases of all of the type-specific parents with no change other than the grandparent version

甜柠檬 2024-09-22 16:09:19

用这种方法试试?

mvn package -Dmaven.test.skip=true -Dwar

<project ×××××>
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>××××</groupId>
    <artifactId>×××××</artifactId>
    <version>×××××</version>
    <relativePath>../../</relativePath>
</parent>
<artifactId>×××××</artifactId>
<name>${project.artifactId}-${project.version}</name>
<description>${project.artifactId}-${project.version}</description>
<properties>
    <packaging.type>jar</packaging.type>
</properties>
<profiles>
    <profile>
        <activation>
            <property>
                <name>war</name>
            </property>
        </activation>
        <properties>
            <packaging.type>war</packaging.type>
        </properties>
        <build>
            <finalName>ROOT</finalName>
        </build>
    </profile>
</profiles>
<packaging>${packaging.type}</packaging>
<dependencies>
    <dependency>
        ... ...
    </dependency>
    ... ... 
</dependencies>

Try in this way ?

mvn package -Dmaven.test.skip=true -Dwar

<project ×××××>
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>××××</groupId>
    <artifactId>×××××</artifactId>
    <version>×××××</version>
    <relativePath>../../</relativePath>
</parent>
<artifactId>×××××</artifactId>
<name>${project.artifactId}-${project.version}</name>
<description>${project.artifactId}-${project.version}</description>
<properties>
    <packaging.type>jar</packaging.type>
</properties>
<profiles>
    <profile>
        <activation>
            <property>
                <name>war</name>
            </property>
        </activation>
        <properties>
            <packaging.type>war</packaging.type>
        </properties>
        <build>
            <finalName>ROOT</finalName>
        </build>
    </profile>
</profiles>
<packaging>${packaging.type}</packaging>
<dependencies>
    <dependency>
        ... ...
    </dependency>
    ... ... 
</dependencies>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文