【问题标题】:Sharing common projects with Maven与 Maven 共享常用项目
【发布时间】:2016-06-01 22:58:36
【问题描述】:

我正在处理重组我们的 Maven 构建流程以在我们所有项目之间共享公共项目的问题,但我无法让它工作。

到目前为止,我们有五个 GUI(带有 JavaFX 的 RCP E4),总共使用了 30 个库。这些部分是我们的(源代码)和第 3 方(罐子)。每个 GUI 都有自己的(工作的)Maven 构建过程。每个 GUI 都有自己的 Git,其中包含编译所需的每个库的副本。为了让您了解依赖项的外观(只有几个 GUI 和几个常见的库):

现在我想摆脱这些项目中的冗余。正如我所说,每个 GUI 都有自己的 Git 和所需公共项目的自己的副本(!)。我已经为公共库创建了 Git 来分离它们。现在我的问题来了。到目前为止,这些常见项目中的每一个都有自己的pom.xml,我需要在其中指定一个父项。这是一个例子pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>gui.a.e4</groupId>
        <artifactId>gui.a.e4.app.releng</artifactId>
        <relativePath>../gui.a.e4.app.releng/pom.xml</relativePath>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
  <groupId>gui.a.e4</groupId>
  <artifactId>common.project.x</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>eclipse-plugin</packaging>
</project>

起初我试图将common.project.x 包含在gui.b.e4 中,导致以下错误:

[INFO] Scanning for projects...
[ERROR] Internal error: java.lang.RuntimeException: Could not resolve target platform specification artifact gui.a:gui.a.app.target:target:1.0.0-SNAPSHOT -> [Help 1]
org.apache.maven.InternalErrorException: Internal error: java.lang.RuntimeException: Could not resolve target platform specification artifact gui.a:gui.a.app.target:target:1.0.0-SNAPSHOT
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:168)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: java.lang.RuntimeException: Could not resolve target platform specification artifact gui.a:gui.a.app.target:target:1.0.0-SNAPSHOT
    at org.eclipse.tycho.core.resolver.DefaultTargetPlatformConfigurationReader.addTargetArtifact(DefaultTargetPlatformConfigurationReader.java:389)
    at org.eclipse.tycho.core.resolver.DefaultTargetPlatformConfigurationReader.setTarget(DefaultTargetPlatformConfigurationReader.java:342)
    at org.eclipse.tycho.core.resolver.DefaultTargetPlatformConfigurationReader.getTargetPlatformConfiguration(DefaultTargetPlatformConfigurationReader.java:75)
    at org.eclipse.tycho.core.resolver.DefaultTychoResolver.setupProject(DefaultTychoResolver.java:86)
    at org.eclipse.tycho.core.maven.TychoMavenLifecycleParticipant.afterProjectsRead(TychoMavenLifecycleParticipant.java:90)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:274)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    ... 11 more
[ERROR]

这个错误是由于 Maven 试图找到 common.project.x 的父级并尝试解决它以及在这种情况下需要创建目标平台的父级的父级(一些 RCP E4 特定的东西)。我第一次尝试解决这个问题是从pom.xml 中删除父级。因此,当我没有在 common.project.x 中指定父级时,gui.a.e4 的构建会抱怨:

[INFO] Scanning for projects...
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]   
[ERROR]   The project auv.e4:art.util.geo:1.0.0-SNAPSHOT (/home/mhoffmann/apps/workspace_rcp_gui/art.util.geo.bundle/pom.xml) has 1 error
[ERROR]     Unknown packaging: eclipse-plugin @ line 8, column 14
[ERROR]

如果我之后将包装类型更改为pom 其他项目找不到这个项目:

[ERROR] Cannot resolve project dependencies:
[ERROR]   Software being installed: gui.a.e4.application 1.0.0.qualifier
[ERROR]   Missing requirement: common.project.y 1.0.0.qualifier requires 'bundle common.project.x 0.0.0' but it could not be found
[ERROR]   Cannot satisfy dependency: gui.a.e4.application 1.0.0.qualifier depends on: bundle common.project.y 0.0.0
[ERROR]

这是用于构建我们的 GUI 之一的 maven XML,它们都是相同的(项目名称除外)

<?xml version="1.0" encoding="UTF-8"?>
<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>
        <name>gui.a - releng</name>
        <prerequisites>
                <maven>3.0</maven>
        </prerequisites>
        <groupId>gui.a</groupId>
        <artifactId>gui.a.app.releng</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>pom</packaging>
        <properties>
                <tycho-version>0.23.1</tycho-version>
                <junit-version>4.11</junit-version>
                <mockito-version>1.8.4</mockito-version>
                <platform-version>4.2</platform-version>
                <efx-version>1.0.0</efx-version>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <show-eclipse-log>true</show-eclipse-log>
        </properties>
        <modules>
                <module>../gui.a.app</module>
                <module>../gui.a.app.feature</module>
                <module>../gui.a.app.product</module>
                <module>../gui.a.specific.project.a</module>
                <module>../gui.a.specific.project.b</module>
                <module>../common.project.a</module>
                <module>../common.project.b</module>
        </modules>
                <pluginRepositories>
                <pluginRepository>
                        <id>tycho</id>
                        <url>http://repository.sonatype.org/content/groups/sonatype-public-grid</url>
                        <snapshots>
                                <enabled>true</enabled>
                        </snapshots>
                </pluginRepository>
        </pluginRepositories>
        <build>
                <!-- build plugins -->
                <plugins>
                        <plugin>
                                <groupId>org.eclipse.tycho</groupId>
                                <artifactId>tycho-maven-plugin</artifactId>
                                <version>${tycho-version}</version>
                                <extensions>true</extensions>
                        </plugin>
                        <plugin>
                                <groupId>org.eclipse.tycho</groupId>
                                <artifactId>target-platform-configuration</artifactId>
                                <version>${tycho-version}</version>
                                <configuration>
                                        <target>
                                                <artifact>
                                                        <groupId>gui.a</groupId>
                                                        <artifactId>gui.a.app.target</artifactId>
                                                        <version>1.0.0-SNAPSHOT</version>
                                                </artifact>
                                        </target>
                                        <resolver>p2</resolver>

                                        <pomDependencies>consider</pomDependencies>
                                        <environments>
                                                <environment>
                                                        <os>win32</os>
                                                        <ws>win32</ws>
                                                        <arch>x86</arch>
                                                </environment>
                                                <environment>
                                                        <os>win32</os>
                                                        <ws>win32</ws>
                                                        <arch>x86_64</arch>
                                                </environment>
                                                <environment>
                                                        <os>linux</os>
                                                        <ws>gtk</ws>
                                                        <arch>x86_64</arch>
                                                </environment>
                                                <environment>
                                                    <os>macosx</os>
                                                    <ws>cocoa</ws>
                                                    <arch>x86_64</arch>
                                            </environment>
                                        </environments>
                                </configuration>
                        </plugin>
                </plugins>
                <!-- defines the default settings for the used plugins -->
                <pluginManagement>
                        <plugins>
                                <plugin>
                                        <groupId>org.eclipse.tycho</groupId>
                                        <artifactId>tycho-compiler-plugin</artifactId>
                                        <version>${tycho-version}</version>
                                        <configuration>
                                                <encoding>UTF-8</encoding>
                                                <source>1.8</source>
                                                <target>1.8</target>
                                                <extraClasspathElements>
                                                        <extraClasspathElement>
                                                                <groupId>javafx</groupId>
                                                                <artifactId>javafx.mvn</artifactId>
                                                                <version>2.2.0-SNAPSHOT</version>
                                                        </extraClasspathElement>
                                                </extraClasspathElements>
                                        </configuration>
                                </plugin>
                                <plugin>
                                        <groupId>org.eclipse.tycho</groupId>
                                        <artifactId>tycho-source-plugin</artifactId>
                                        <version>${tycho-version}</version>
                                        <executions>
                                                <execution>
                                                        <id>plugin-source</id>
                                                        <goals>
                                                                <goal>plugin-source</goal>
                                                        </goals>
                                                </execution>
                                        </executions>
                                </plugin>
                                <plugin>
                                        <groupId>org.eclipse.tycho</groupId>
                                        <artifactId>tycho-packaging-plugin</artifactId>
                                        <version>${tycho-version}</version>
                                        <configuration>
                                                <archive>
                                                        <addMavenDescriptor>false</addMavenDescriptor>
                                                </archive>
                                        </configuration>
                                </plugin>
                        </plugins>
                </pluginManagement>
        </build>
        <dependencyManagement>
                <dependencies>
                        <dependency>
                                <groupId>junit</groupId>
                                <artifactId>junit</artifactId>
                                <version>${junit-version}</version>
                                <scope>test</scope>
                        </dependency>
                </dependencies>
        </dependencyManagement>
        <dependencies>
                <dependency>
                        <groupId>org.mockito</groupId>
                        <artifactId>mockito-core</artifactId>
                        <version>${mockito-version}</version>
                </dependency>
                <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <scope>test</scope>
                </dependency>
        </dependencies>
</project>

我也不知道常见项目的 POM 应该是什么样子或做什么。我对 Maven 了解不多,仅适用于调整现有脚本以包含更多 common.projects :-)。到目前为止,我不知道我应该改变什么才能使其正常工作。请帮助我,Maven让我发疯。 :-)

【问题讨论】:

    标签: maven eclipse-rcp tycho e4


    【解决方案1】:

    图表中的箭头表示层次结构(最顶层位于图表底部)。考虑到将common.project.x 声明为 GUI 的子模块 (&lt;module&gt;../common.project.a&lt;/module&gt;) 是违反直觉的。特别是如果两个 GUI 引用相同的 common.project.x

    我建议采用以下结构:

    + main
      +- pom.xml  ... declarations common to all projects, common and gui as sub-modules
      +- common
         +- pom.xml  ... declarations common to common.projects, common.projects as sub-modules
         +- common.project.a
            +- pom.xml
         +- common.project.b
            +- pom.xml  ... including direct dependency to a
         +- ...
            +- ... 
      +- gui
         +- pom.xml  ... declarations common to GUI projects, GUI projects as sub-modules
         +- GUI a
            +- pom.xml  ... including direct dependencies to b, c
         +- GUI b
            +- pom.xml  ... including direct dependencies to b, c, e
         +- ...
            +- ... 
    

    每个项目的直接祖先都是&lt;parent&gt;

    注意省略传递依赖:

    • aGUI a
    • adGUI b

    另见:

    【讨论】:

    • 我不知道这是可能的(使用 maven),这当然会让一切变得更干净。但是 pom.xml 会是什么样子,尤其是主要的 pom.xml。我想对于 GUI x pom.xml 我只需要将主要的指定为它的父项,对吗?对常见的项目也这样做。
    • @Westranger 如前所述,main POM 应包括所有后代项目继承的所有声明(以及 &lt;pluginManagement&gt;&lt;dependencyManagement&gt; 部分)。你猜错了。 ;) 所有GUI x 项目都应该有gui 作为父级。只是 commonguimain 作为父级。我将为我的答案添加一些参考。
    • 现在我发现我在这里误解了 parent 的含义 :-)。如果在构建 GUI a 时 GUI b 不存在,现在会发生什么。在构建 GUI a 时,我不想检查 GUI b,对我来说,看起来 maven 现在会抛出一个错误(只是猜测),即 GUI b 不存在。最后,我只想检查 GUI a 和必要的公共项目,然后构建它,这适用于您的解决方案吗?
    • @Westranger 如果您只是在其目录中构建GUI a,这将有效,因为GUI a 不依赖于GUI b。当然,它所依赖的common.projects 必须在至少一次之前构建(至少使用mvn install)。如果缺少任何GUI 项目,如果在各自的目录中构建guimain(自动包括其子模块),Maven 将抛出错误。
    • 我会尝试适当地更改我们所有的脚本并让您知道结果。 Vielen lieben Dank :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 2019-12-22
    相关资源
    最近更新 更多