如何在Maven中使用配置文件,分类器和模块正常工作?

时间:2017-01-20 09:50:03

标签: java maven

我尝试创建一个支持两个不同环境的相同代码的Maven配置。一个环境是带有Seam的Java 6,另一个是带有JEE的Java 7。

我的项目是一个包含父pom的多模块项目,我在其中配置了两个配置文件。父pom中有趣的部分:

<profiles>
    <profile>
        <id>JDK7</id>
        <properties>
            <jdk.version>1.7</jdk.version>
            <profile.classifier>with-beans-xml</profile.classifier>
        </properties>

        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>${jdk.version}</source>
                        <target>${jdk.version}</target>
                        <encoding>${project.build.sourceEncoding}</encoding>
                    </configuration>
                </plugin>

                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.4</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                            <configuration>
                                <classifier>${profile.classifier}</classifier>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>    
    </profile>

    <profile>
        <id>JDK6</id>
        <properties>
            <jdk.version>1.6</jdk.version>
            <profile.classifier>without-beans-xml</profile.classifier>
        </properties>

        <!-- Identical parts left out for brevity -->

        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                            <configuration>
                                <classifier>${profile.classifier}</classifier>
                                <excludes>
                                    <exclude>src/main/resources/META-INF/beans.xml</exclude>
                                </excludes>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>    
    </profile>
</profiles>

在这个pom的子模块中,我需要引用其他模块的特定版本,以确保它们都使用相同的Java版本,并且应该包含或不包含beans.xml。

我就是这样做的:

<parent>
    <groupId>commons</groupId>
    <artifactId>parent-pom</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>some-module</artifactId>

<dependencies>
    <dependency>
        <groupId>commons</groupId>
        <artifactId>core</artifactId>
        <version>${project.version}</version>
        <classifier>${profile.classifier}</classifier>
    </dependency>
</dependencies>

上面解析的属性解析得很好,但上面的core依赖包含对包含整个项目核心功能的其他模块的引用。

当我将该模块导入其他模块时,core中的依赖关系无法解决。

这是我期待的依赖层次结构:

- some-module
    - core
        - some-other-module

但是,在将core作为依赖项添加到另一个模块之后,我明确需要添加some-other-module以使其可用,即some-other-module不能通过引用core传递。 / p>

我是以错误的方式来做这件事的吗?它适用于不同的版本,只是我必须明确地添加它们,而不是在其他用户打算使用它时避免混淆。

更新

根据请求,这里是完整的父pom和其中一个遇到传递依赖问题的模块。

父pom.xml

<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>se.commons</groupId>
    <artifactId>healthcheck-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>healthcheck-parent-pom</name>

    <modules>
        <module>healthcheck-core</module>
        <module>healthcheck-xsd</module>
        <module>healthcheck-jms</module>
        <module>healthcheck-db</module>
        <module>healthcheck-filesystem</module>
        <module>healthcheck-http</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.home>${JAVA_1_7_HOME}</java.home>
    </properties>

    <profiles>
        <profile>
            <id>JDK7</id>
            <!-- <activation> -->
            <!-- <activeByDefault>true</activeByDefault> -->
            <!-- </activation> -->
            <properties>
                <!-- Build properties -->
                <jdk.version>1.7</jdk.version>
                <profile.classifier>with-beans-xml</profile.classifier>

                <bom.version>2.0.0.R</bom.version>
            </properties>

            <dependencyManagement>
                <dependencies>
                    <dependency>
                        <groupId>se.commons</groupId>
                        <artifactId>bom</artifactId>
                        <version>${bom.version}</version>
                        <scope>import</scope>
                        <type>pom</type>
                    </dependency>
                </dependencies>
            </dependencyManagement>

            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.3</version>
                        <configuration>
                            <source>${jdk.version}</source>
                            <target>${jdk.version}</target>
                            <encoding>${project.build.sourceEncoding}</encoding>
                        </configuration>
                    </plugin>

                    <plugin>
                        <artifactId>maven-jar-plugin</artifactId>
                        <version>2.4</version>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                                <configuration>
                                    <classifier>${profile.classifier}</classifier>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>findbugs-maven-plugin</artifactId>
                        <version>3.0.1</version>
                        <configuration>
                            <effort>Default</effort>
                            <threshold>Default</threshold>
                            <xmlOutputDirectory>target/findbugs</xmlOutputDirectory>
                            <failOnError>true</failOnError>
                            <xmlOutput>true</xmlOutput>
                            <findbugsXmlOutput>true</findbugsXmlOutput>
                            <findbugsXmlWithMessages>true</findbugsXmlWithMessages>
                            <targetJdk>${jdk.version}</targetJdk>
                            <configuration>
                                <omitVisitors>FindReturnRef</omitVisitors>
                            </configuration>
                        </configuration>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.18.1</version>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-checkstyle-plugin</artifactId>
                        <version>2.15</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>checkstyle</goal>
                                </goals>
                                <phase>compile</phase>
                            </execution>
                        </executions>
                        <configuration>
                            <configLocation>${basedir}/../src/site/checkstyle.xml</configLocation>
                        </configuration>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <version>2.2.1</version>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.8</version>
                        <executions>
                            <execution>
                                <id>attach-javadocs</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-site-plugin</artifactId>
                        <version>3.3</version>
                        <dependencies>
                            <dependency>
                                <!-- add support for ssh/scp -->
                                <groupId>org.apache.maven.wagon</groupId>
                                <artifactId>wagon-ssh</artifactId>
                                <version>2.4</version>
                            </dependency>
                        </dependencies>
                        <configuration>
                            <locales>sv</locales>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>


        <profile>
            <id>JDK6</id>
            <properties>
                <!-- Build properties -->
                <jdk.version>1.6</jdk.version>
                <profile.classifier>without-beans-xml</profile.classifier>

                <bom.version>2.0.0.R</bom.version>
            </properties>

            <dependencyManagement>
                <dependencies>
                    <dependency>
                        <groupId>se.commons</groupId>
                        <artifactId>bom</artifactId>
                        <version>${bom.version}</version>
                        <scope>import</scope>
                        <type>pom</type>
                    </dependency>
                </dependencies>
            </dependencyManagement>

            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources/META-INF</directory>
                        <excludes>
                            <exclude>beans.xml</exclude>
                        </excludes>
                    </resource>
                </resources>

                <plugins>
                    <plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.3</version>
                        <configuration>
                            <source>${jdk.version}</source>
                            <target>${jdk.version}</target>
                            <encoding>${project.build.sourceEncoding}</encoding>
                        </configuration>
                    </plugin>

                    <plugin>
                        <artifactId>maven-jar-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                                <configuration>
                                    <classifier>${profile.classifier}</classifier>
                                    <excludes>
                                        <exclude>src/main/resources/META-INF/beans.xml</exclude>
                                    </excludes>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>findbugs-maven-plugin</artifactId>
                        <version>3.0.1</version>
                        <configuration>
                            <effort>Default</effort>
                            <threshold>Default</threshold>
                            <xmlOutputDirectory>target/findbugs</xmlOutputDirectory>
                            <failOnError>true</failOnError>
                            <xmlOutput>true</xmlOutput>
                            <findbugsXmlOutput>true</findbugsXmlOutput>
                            <findbugsXmlWithMessages>true</findbugsXmlWithMessages>
                            <targetJdk>${jdk.version}</targetJdk>
                            <configuration>
                                <omitVisitors>FindReturnRef</omitVisitors>
                            </configuration>
                        </configuration>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.18.1</version>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-checkstyle-plugin</artifactId>
                        <version>2.15</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>checkstyle</goal>
                                </goals>
                                <phase>compile</phase>
                            </execution>
                        </executions>
                        <configuration>
                            <configLocation>${basedir}/../src/site/checkstyle.xml</configLocation>
                        </configuration>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <version>2.2.1</version>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.8</version>
                        <executions>
                            <execution>
                                <id>attach-javadocs</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-site-plugin</artifactId>
                        <version>3.3</version>
                        <dependencies>
                            <dependency><!-- add support for ssh/scp -->
                                <groupId>org.apache.maven.wagon</groupId>
                                <artifactId>wagon-ssh</artifactId>
                                <version>2.4</version>
                            </dependency>
                        </dependencies>
                        <configuration>
                            <locales>sv</locales>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>


    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>2.4</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
                <version>2.18.1</version>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <effort>Default</effort>
                    <threshold>Default</threshold>
                    <xmlOutputDirectory>target/findbugs</xmlOutputDirectory>
                    <failOnError>true</failOnError>
                    <xmlOutput>true</xmlOutput>
                    <findbugsXmlOutput>true</findbugsXmlOutput>
                    <findbugsXmlWithMessages>true</findbugsXmlWithMessages>
                    <targetJdk>${jdk.version}</targetJdk>
                    <configuration>
                        <omitVisitors>FindReturnRef</omitVisitors>
                    </configuration>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.4</version>
                <configuration>
                    <targetJdk>${jdk.version}</targetJdk>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.7</version>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>cobertura</report>
                        </reports>
                    </reportSet>
                </reportSets>
                <configuration>
                    <instrumentation>
                    </instrumentation>
                    <formats>
                        <format>xml</format>
                        <format>html</format>
                    </formats>
                    <instrumentation>
                        <includes>
                            <include>**/*.class</include>
                        </includes>
                    </instrumentation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.8</version>
            </plugin>
        </plugins>
    </reporting>
</project>

这是使用核心作为依赖的模块之一:

<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>se.commons</groupId>
        <artifactId>healthcheck-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>healthcheck-jms</artifactId>

    <dependencies>
        <dependency>
            <groupId>se.commons</groupId>
            <artifactId>healthcheck-core</artifactId>
            <version>${project.version}</version>
            <classifier>${profile.classifier}</classifier>
        </dependency>

        <dependency>
            <groupId>org.jboss.spec.javax.ejb</groupId>
            <artifactId>jboss-ejb-api_3.1_spec</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.spec.javax.jms</groupId>
            <artifactId>jboss-jms-api_1.1_spec</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

最后,这是核心的pom:

<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>se.commons</groupId>
        <artifactId>healthcheck-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>healthcheck-core</artifactId>

    <dependencies>
        <dependency>
            <groupId>se.commons</groupId>
            <artifactId>healthcheck-xsd</artifactId>
            <version>${project.version}</version>
            <classifier>${profile.classifier}</classifier>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>

1 个答案:

答案 0 :(得分:0)

在找到类似的问题后,我终于放弃了上述解决方案,从未得到明确答案。

似乎无法使用分类器(仅使用标准GAV)来解决传递依赖性。

我决定使用另一个artifactId来区分项目。