子模块与settings.xml属性的变量依赖关系

时间:2013-09-29 14:04:36

标签: java xml maven pom.xml

我有一个具有以下结构的示例maven项目:

parent
   frontend
   backend

前端取决于后端。 后端具有用于测试的数据库驱动程序依赖项。但是,此依赖项应取决于开发人员settings.xml

    <dependency>
        <groupId>${database.driver.groupId}</groupId>
        <artifactId>${database.driver.artifactId}</artifactId>
        <version>${database.driver.version}</version>
        <scope>test</scope>
    </dependency>

在我的settings.xml中,我有这个:

    <profile>
        <id>database</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <database.driver.groupId>mysql</database.driver.groupId>
            <database.driver.artifactId>mysql-connector-java</database.driver.artifactId>
            <database.driver.version>5.1.9</database.driver.version>
        </properties>
    </profile>

当我只运行后端时,属性被正确替换。 但是,如果我运行前端(或父)传递依赖项不可用于前端,因为它不能再替换属性:

 [WARNING] The POM for de.phil.mvntest:business:jar:0.0.1-SNAPSHOT is invalid, transitive dependencies (if any) will not be available: 2 problems were encountered while building the effective model for de.phil.mvntest:business:0.0.1-SNAPSHOT
 [ERROR] 'dependencies.dependency.artifactId' for ${database.driver.groupId}:${database.driver.artifactId}:jar with value '${database.driver.artifactId}' does not match a valid id pattern. @ 
 [ERROR] 'dependencies.dependency.groupId' for ${database.driver.groupId}:${database.driver.artifactId}:jar with value '${database.driver.groupId}' does not match a valid id pattern. @ 

有趣的是,如果我将配置文件声明移动到父的pom.xml中,它可以正常工作!

所以我的问题是,为什么子模块的属性在来自settings.xml时不会被替换。

注意: “help:active-profiles -N”显示settings.xml中的配置文件处于活动状态。

注2:Maven版本附带STS并且是Embedded 3.0.4


关注settins.xml和poms

后端

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>de.phil.mvntest</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>backend</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>${database.driver.groupId}</groupId>
            <artifactId>${database.driver.artifactId}</artifactId>
            <version>${database.driver.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

fontend

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>de.phil.mvntest</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>frontend</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>de.phil.mvntest</groupId>
            <artifactId>backend</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>de.phil.mvntest</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <modules>
        <module>backend</module>
        <module>frontend</module>
    </modules>
</project>

的settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
    <profiles>
        <profile>
            <id>database</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <database.driver.groupId>mysql</database.driver.groupId>
                <database.driver.artifactId>mysql-connector-java</database.driver.artifactId>
                <database.driver.version>5.1.9</database.driver.version>
            </properties>
        </profile>
    </profiles>
</settings>

1 个答案:

答案 0 :(得分:0)

在配置文件中处理依赖项不是很安全。它运行得相当好的唯一情况是针对不同的依赖项运行测试,甚至使用maven-invoker-plugin更安全。部署的pom只能设置一个依赖项。