【问题标题】:maven profile conditional dependencymaven配置文件条件依赖
【发布时间】:2014-06-11 23:22:28
【问题描述】:

我有一个 pom 文件,其中有 1 个条件依赖项和许多其他非条件依赖项(即,我希望两个配置文件都存在这些依赖项)。

条件是我是否在 tomcat 或 websphere 上进行部署,因此我已相应地命名了我的 maven 配置文件。

我的问题是,我如何才能根据我的个人资料使单个依赖项成为条件,并且两个配置文件都存在其他依赖项?

我试了一下,并在下面粘贴了我尝试过的内容,但我所尝试的内容似乎不起作用。欢迎提供一些反馈。

谢谢

<!-- tried to put the conditional dependencies in the profile section -->
<profiles>
    <profile>
    <id>tomcat</id>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>        
    </profile>
    <profile>
     <id>websphere</id>
     <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
     </dependencyManagement>
    </profile>
</profiles>

<!-- then list all other non-conditional dependencies -->
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    ...
    ...
</dependencies>

【问题讨论】:

  • 您希望哪一个可用于两个配置文件?为什么不把它放在非条件依赖中?
  • 不确定您所说的似乎不起作用是什么意思?您有 2 个配置文件,因此应该根据配置文件依赖项获取

标签: maven


【解决方案1】:

您的个人资料中不应该有依赖管理部分。依赖管理用于创建具有默认版本的依赖列表,您在模块的依赖部分中引用这些依赖列表。您希望在您的配置文件中有一个依赖项部分。两台服务器上使用的公共依赖项应列在顶级依赖项部分。

但是,这仍然是一个糟糕的模式,因为您不能在环境之间促进单个构建。如果您将容器依赖的依赖项设置为provided 范围,然后将库单独部署到服务器会更好。

另外,我假设您在此处的代码仅用于演示目的,因为两个容器上的 jstl 依赖项是相同的。

【讨论】:

  • 非常感谢。那是一个很好的答案。我已经更改了 xml,以便将依赖项标记作为配置文件标记的子项,而不是依赖项管理标记,并删除了依赖项管理标记。尽管 websphere 的 jstl 依赖项是相同的,但提供了范围,但对于 tomcat 则没有,因此我尝试使用 maven 配置文件找到解决方案。多亏你自己,它运作良好。
  • 哦,还有关于您无法通过环境推广构建的说明。我们使用 tomcat 进行本地构建,但使用 websphere 进行产品构建。幸运的是,对于 websphere,我们有多个非生产环境来促进 websphere 在进入 prod 之前构建。
猜你喜欢
  • 2011-05-05
  • 1970-01-01
  • 2012-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-10
相关资源
最近更新 更多