与Maven共享常见项目

时间:2016-06-01 08:04:19

标签: maven eclipse-rcp tycho e4

我正在应对重构Maven构建流程的问题,以便在我们所有项目中共享共同项目,但我无法让它发挥作用。

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

enter image description here

现在我想摆脱这些项目的冗余。正如我所说的,每个GUI都有自己的Git和它自己的所需公共项目的副本(!)。我已经为公共库创建了Gits以将它们分开。现在来了我的问题。到目前为止,每个这些常见项目都有自己的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.project s :-)。到目前为止,我不知道应该改变什么才能让它发挥作用。请帮帮我,Maven让我疯狂。 :-)

1 个答案:

答案 0 :(得分:0)

图表中的箭头表示层次结构(图表底部的最顶层)。考虑到这一点,声明common.project.x作为GUI的子模块(<module>../common.project.a</module>)是违反直觉的。特别是如果两个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
     +- ...
        +- ... 

每个项目都有其直接祖先<parent>

请注意省略传递依赖项:

  • a GUI a
  • ad GUI b

另见: