使用 Maven,我如何使用一个不同的 .java 文件构建两个版本的 Java 项目? [英] With Maven how do I build two versions of a Java project with just one different .java file?

查看:48
本文介绍了使用 Maven,我如何使用一个不同的 .java 文件构建两个版本的 Java 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个应用程序的两个版本,唯一的区别是我想交换一个特定 .java 文件的不同版本.我已经有一个用于构建源代码的 Maven 构建,加上一个 Ant 构建,然后将其打包为带有嵌入式 JRE 的 zip.

I want to build two versions of an application, the only difference is I want to swap in a different version of one particular .java file. I already have a Maven build for building the source, plus an Ant build to then package up as a zip with an embedded JRE.

所以我可以修改我的 Ant 构建来为这个新构建创建另一个 zip 文件,但是用单个修改后的源文件处理源重建的最佳方法是什么?

So I can modify my Ant build to create another zip file for this new build but what would be the best way to handle the rebuilding of the source with the single amended source file?

更新进度

我现在已经使用 main() 方法对起始类进行了子类化,所以不再需要交换源文件,而我们只需要为 mainClass 在清单中,我们现在只需要构建一个源代码树.

I have now subclassed the starting class with the main() method, so no longer have to swap source files around instead we simply need to pass a difference value for the mainClass in the manifest and we now only have one source tree to build.

所以在我的 pom.xml 中我有:

So in my pom.xml I have :

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.3</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <descriptors>
                <descriptor>assembly.xml</descriptor>
            </descriptors>
            <archive>
                <manifest>
                                  <mainClass>com.companyname.StartClass</mainClass>
                    <packageName>com.companyname</packageName>
                    <addClasspath>true</addClasspath>
                </manifest>
            </archive>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

那么我是否可以拥有多个这样的程序集插件,以便我可以构建两个仅在清单中不同的程序集?

So can I have multiple of these assembly plugins so I can build two assemblies just differing in the manifest?

推荐答案

基本上这违背了 Maven 的一些核心概念:

Basically this is against a few of Maven's core concepts:

  • 一个项目有一个 / 导致一个工件.
  • 具有相同名称的工件( 默认派生自 )是应该有相同的内容.
  • One project has one <build>/<sourceDirectory> that leads to one artifact.
  • An artifact with the same name (<finalName> derived from <artifactId> and <version> by default) is supposed to have the same content.

有几个解决方案:

  • 来自属性文件的属性一个 if 来实现不同的运行时行为.你不喜欢那样.
  • 通过命令行定义的 property一个 if.
  • 一个来自资源文件的文本,在一个 if.你不喜欢那样.
  • 命令行参数,它在 <代码>如果.
  • 一个 Java 断言,但是从那里引用:不要使用断言来执行您的应用程序正确操作所需的任何工作.".
  • A property from a property file that's evaluated in an if to achieve different runtime behaviour. You don't prefer that.
  • A property defined via command line that's evaluated in an if.
  • A text from a resource file that's evaluated in an if. You don't prefer that.
  • A command line argument that's evaluated in an if.
  • A Java assertion, but cited from there: "Do not use assertions to do any work that your application requires for correct operation.".

一种 Maven 方法是:

One Maven way would be:

  • 除了您当前的 (A) 之外,还要创建两个额外的项目 (B、C).A 可以是 B 和 C 的父级以继承公共声明.
  • B 和 C 只包含不同的源文件.
  • 在 A 的 POM 中引入两个 profiles 并选择各种激活方法之一:
  • Create two additional projects (B, C) in addition to your current (A). A can be the parent of B and C to inherit common declarations.
  • B and C contain just the different source files.
  • Introduce two profiles in A's POM with <dependencies> and select one of the various activation methods:

 

  <profiles>

    <profile>
      <id>B</id>
      <activation>
        ...
      </activation>
      <dependencies>
        <dependency>
          <groupId>com.example</groupId>
          <artifactId>B</artifactId>
          <version>...</version>              
        </dependency>
      </dependencies>
      <build>
        <finalName>AwithB</finalName>
      </build>
    </profile>

    <profile>
      <id>C</id>
      <activation>
        ...
      </activation>
      <dependencies>
        <dependency>
          <groupId>com.example</groupId>
          <artifactId>C</artifactId>
          <version>...</version>              
        </dependency>
      </dependencies>
      <build>
        <finalName>AwithC</finalName>
      </build>
    </profile>

  </profiles>

记住开头的第二个概念:相同的名称,相同的内容(和行为).您可以在配置文件中使用 声明来相应地调整 A 的名称.

Remember the second concept at the beginning: same name, same content (and behaviour). You can use the <finalName> declaration in the profiles to adapt A's name accordingly.

这篇关于使用 Maven,我如何使用一个不同的 .java 文件构建两个版本的 Java 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆