1

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.

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?

Updated With Progress

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.

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?

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • How do you change this source file between builds? Is it possible to achieve different runtime behaviour by accessing a resource config file? BTW, You don't have to use a separate Ant build. The [Maven Assembly Plugin](https://maven.apache.org/plugins/maven-assembly-plugin/) can be configured in your project's POM to do exactly this. – Gerold Broser Feb 17 '17 at 01:03
  • @GeroldBrosser 'How do you change this source file between builds? ' that is the crux of the matter, and for me a better solution than using a config file to achieve the difference. – Paul Taylor Feb 17 '17 at 08:35
  • Actually I meant how you change the content of the source file alternately. Do you overwrite it from two different locations every time? Do you change it manually? – Gerold Broser Feb 17 '17 at 10:55
  • @GeroldBroser I havent done it yet thats why Im asking the question. – Paul Taylor Feb 17 '17 at 11:03
  • @GeroldBroser I cant get profiles to work , posted a nother simpler question if you could help http://stackoverflow.com/questions/42299040/how-do-i-make-this-maven-pom-have-two-profiles-only-difference-being-in-mainclas please – Paul Taylor Feb 17 '17 at 13:21

1 Answers1

0

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.

There are a few solutions:

  • 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.".

One Maven way would be:

  • 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>

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.

Community
  • 1
  • 1
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107