【问题标题】:Build java solution with 4 configurations in one call一次调用构建具有 4 个配置的 java 解决方案
【发布时间】:2013-12-09 11:37:41
【问题描述】:

我有 5 个项目的解决方案:

  • 00-PROJECT - 整个解决方案的父项目。其他项目是该项目的子模型。
  • 01-INTERFACE - 用于客户端和服务器之间通信的 API 描述库
  • 02-SERVER - 提供 API 的服务器应用程序
  • 03-CLIENT - 与服务器应用程序通信的客户端模块
  • 04-DESKTOP - 使用 SWT GUI 库构建的桌面应用程序。使用来自 03-CLIENT 的客户端模块与服务器通信。

在我开发整个解决方案时,我需要在 04-DESKTOP 中嵌入 02-SERVER。这是为了在 Eclipse IDE 中轻松运行和调试应用程序所需要的,因为客户端和服务器都在一起开发中。

但要将应用程序部署到客户端,我需要分别构建 02-SERVER04-DESKTOP。我找到了准备部署 .zip 的解决方案 - 它是 maven-assembly-plugin。它将所有 .jar、配置文件、jasper 模板组装在一个 .zip(或文件夹)中。它工作完美。但是当我尝试为几个平台创建04-DESKTOP的部署包时出现了一个问题,至少这三个平台:Windows 32bit,Windows 64bit,Linux 64bit(都在客户组织中使用)。

04-DESKTOP 的解决方案之一是配置文件。 Maven 提供了一个强大的配置文件系统。但问题是您可以使用一个配置文件运行 maven,并且无法为多个配置文件调用 maven。第二个想法是我调用 maven 为每个配置文件构建包,使用父 pom 02-SERVER 每次都重新编译,我不喜欢这个解决方案。

我的主要问题是如何使用单个命令准备 5 个包(一个服务器包和 4 个桌面包)?例如在父项目中调用mvn package

【问题讨论】:

    标签: java eclipse maven deployment swt


    【解决方案1】:

    我搜索了一段时间的答案。以下是 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。

    也许这个解决方案是错误的,也许不是那么正确,但我很高兴。我真的希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 2016-08-28
      • 2017-08-01
      • 2012-02-23
      • 1970-01-01
      相关资源
      最近更新 更多