我搜索了一段时间的答案。以下是 stackoverflow.com 上的类似问题和答案,但我没有找到我的问题的答案。经过一番研究和尝试,我找到了解决方案。
我刚刚为我需要的每个平台在解决方案中添加了 4 个项目。所以我的解决方案现在有 7 个项目:
- 00-项目
- 01-接口
- 02-服务器
- 03-客户
- 04-桌面
- 04-DESKTOP-LINUX-x86
- 04-DESKTOP-LINUX-x64
- 04-DESKTOP-WIN32-x86
- 04-DESKTOP-WIN32-x64
这些附加项目的文件夹中没有源代码。但是这些项目中有pom.xml。构建配置指向04-DESKTOP源文件夹:
<build>
<sourceDirectory>../04-DESKTOP/src</sourceDirectory>
</build>
因为所有这 4 个项目都是主项目中的模块,所以我不能给它们相同的 artifactId。但是maven-jar-plugin 默认创建名称等于 artifactId 的 jar。所以结果jar文件名在maven-jar-plugin中重新定义:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix></classpathPrefix>
<mainClass>net.cloudstock.desktop.app</mainClass>
</manifest>
</archive>
<finalName>net.cloudstock.desktop-${project.version}</finalName>
</configuration>
</plugin>
其他项目之一的整个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>
<parent>
<!-- Information about parent project -->
<artifactId>net.cloudstock.system</artifactId>
<groupId>net.cloudstock</groupId>
<version>1.0.1</version>
</parent>
<artifactId>net.cloudstock.desktop.win32.x86</artifactId>
<name>Cloudstock Descktop frontend (Windows x86)</name>
<packaging>jar</packaging>
<properties>
<!-- Operation system where this app must run -->
<os>win32</os>
<!-- Processor architecture -->
<arch>x86</arch>
<!-- Variable to point sources to 04-DESKTOP/src folder -->
<desktop.root>../04-DESKTOP</desktop.root>
<!-- I am new in maven, so I can't comment this line -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<!-- Tell to the compiler where sources are -->
<sourceDirectory>${desktop.root}/src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix></classpathPrefix>
<mainClass>net.cloudstock.desktop.app</mainClass>
</manifest>
</archive>
<!-- Redefine result jar, because by default its name will be net.cloudstock.desktop.win32.x86-{VERSION}.jar -->
<finalName>net.cloudstock.desktop-${project.version}</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${desktop.root}/assembly.xml</descriptor>
</descriptors>
<!-- the name of the zip archive with program that I send to the customer -->
<finalName>net.cloudstock.desktop-${os}-${arch}-${project.version}</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- SWT for Win x86. It differs for different OSes and processors -->
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.win32.win32.x86</artifactId>
<version>4.3</version>
</dependency>
<!-- Other application deps. They are duplicated in all 04-DESKTOP* projects -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>net.cloudstock</groupId>
<artifactId>net.cloudstock.client</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>net.cloudstock</groupId>
<artifactId>net.cloudstock.interface</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.jface</groupId>
<artifactId>org.eclipse.jface</artifactId>
<version>3.8.0.v20120521-2329</version>
</dependency>
</dependencies>
</project>
这个架构中的一件事不够好是${desktop.root}/assembly.xml中的黑客攻击
您不能告诉 maven-assembly-plugin 在程序集中包含工件 jar,因为它包含带有 artifactId 名称的 jar。所以我添加了一个额外的fileSet 以包含当前项目的结果 jar:
<fileSets>
<fileSet>
<directory>target</directory>
<outputDirectory>lib/</outputDirectory>
<includes>
<include>net.cloudstock.desktop-${project.version}.jar</include>
</includes>
</fileSet>
...
</fileSets>
最后我在04-DESCTOP/pom.xml 中添加了依赖标签以包含02-SERVER,并且四个配置文件被操作系统条件激活:
<profile>
<id>win-x86-debug</id>
<activation>
<activeByDefault>false</activeByDefault>
<os>
<family>Windows</family>
<arch>x86</arch>
</os>
</activation>
<dependencies>
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.win32.win32.x86</artifactId>
<version>4.3</version>
</dependency>
</dependencies>
</profile>
现在我在 Eclipse 的 04-DESCTOP 项目中开发桌面应用程序。我可以运行和调试整个解决方案。
当我完成当前迭代并希望将应用程序发送给客户时,我使用来自父项目mvn package 的一个命令来构建它:
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] ------------------------------------------------------------------------
[INFO] Cloudstock System ..................................... SUCCESS [2.202s]
[INFO] Helper Library by Nicolai Budico ...................... SUCCESS [4.563s]
[INFO] Cloudstock Interface .................................. SUCCESS [2.925s]
[INFO] Cloudstock Server ..................................... SUCCESS [3.835s]
[INFO] Cloudstock Client Backend ............................. SUCCESS [1.120s]
[INFO] Cloudstock Descktop frontend (Windows x86) ............ SUCCESS [9.019s]
[INFO] Cloudstock Descktop frontend (Windows x64) ............ SUCCESS [5.530s]
[INFO] Cloudstock Descktop frontend (Linux x86) .............. SUCCESS [5.158s]
[INFO] Cloudstock Descktop frontend (Linux x64) .............. SUCCESS [4.998s]
[INFO] ------------------------------------------------------------------------
在此解决方案中必须注意的最后一个想法是,如果桌面应用程序配置发生更改,您需要编辑 5 个 pom。
也许这个解决方案是错误的,也许不是那么正确,但我很高兴。我真的希望这对某人有所帮助。