1

When I try to deploy a war or jar file with github actions I can't rename the war/jar file to what I want; I tried changing the name using finalName tag but that didn't work it seems when I install it before hand it gives it the proper name, however when it deploys the name defaults back to artifactId-version, is their a maven commmand that I can put into github actions in order to fix this?

here is the build tag section of my pom file I do have another build tag at the beginning of this code sample, stack overflow isn't showing it for some reason

    <sourceDirectory>WEB-INF/src</sourceDirectory>
    <pluginManagement>
        <plugins>
            <plugin>            
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                 <webXml>WEB-INF\web.xml</webXml>    
                 

                </configuration>
            </plugin>
            
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat6-maven-plugin</artifactId>
                <version>2.2</version>
            </plugin>
            
            <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <executions>
             <execution>
                <id>tomcat-run</id>
                <goals>
                    <goal>exec-war-only</goal>
                </goals>
                <phase>package</phase>
                <configuration>
                    <!-- optional only if you want to use a preconfigured server.xml file -->
                    <!-- <serverXml>src/main/tomcatconf/server.xml</serverXml> -->
                    <warRunDependencies>
                        <warRunDependency>
                            <dependency>
                                <groupId>a groupId</groupId>
                                <artifactId>and artifactId</artifactId>
                                <version>version</version>
                                <type>war</type>
                            </dependency>
                            <contextPath>/</contextPath>
                        </warRunDependency>
                    </warRunDependencies>
                    <!-- naming is disabled by default so use true to enable it -->
                    <enableNaming>true</enableNaming>
                    <!-- extra dependencies to add jdbc driver, mail jars, etc. -->
                    <extraDependencies>
                        <extraDependency>
                            <groupId>org.apache.derby</groupId>
                            <artifactId>derby</artifactId>
                            <version>10.1.3.1</version>
                        </extraDependency>
                        <extraDependency>
                            <groupId>javax.mail</groupId>
                            <artifactId>mail</artifactId>
                            <version>1.4</version>
                        </extraDependency>
                    </extraDependencies>
                </configuration>
            </execution>
        </executions>
        </plugin>
        </plugins>
        
    </pluginManagement>
</build>
4help2
  • 21
  • 3

1 Answers1

-1

Note

All depends of your pom.xml. If you are using custom plugins related to the artifact final name, it would be complicated to override the final name.

Simple Jar (maven solution)

Jar is usually used for libraries and standalone apps. After spring-boot, is used for server side apps.

According to this: https://stackoverflow.com/a/39407739/3957754 you can override the final jar name with:

mvn clean package -Djar.finalName=acmejar

I tested with this simple code and it works!

And then in your yml:

name: Java CI
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'adopt'
      - name: Build with Maven
        run: mvn clean package -Djar.finalName=acmejar verify

Some frameworks with custom maven configurations may cause this method to not work. In that case, use some of these approaches to override the jar name using maven:

How to override maven property in command line?

War, spring-boot, etc (maven solution)

As I said, maven projects with complex plugins, needs special treatment in order to override the final artifact name.

The probed solution is to execute some operations after everything else finishes. I used Apache Maven AntRun Plugin

If I run mvn clean package in this repository, the generated war is: jersey-simple-1.0.0.war

img1

But if I add this plugin:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <configuration>
                <tasks>
                    <copy file="${project.build.directory}/${project.build.finalName}.war" 
                        tofile="${project.build.directory}/${newName}.war"/>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

And run this: mvn clean package -DnewName=foo

A new war name is generated:

img2

You could use the move task if you don't need to have the initial file:

<move file="file.orig" tofile="file.moved"/>

Finally add this line to your github action yml.

This is the repository with the ant run plugin.

Pure unix commands

You can run basic unix commands in Github Actions.

So if you know the initial name after the success mvn clean package, you could use the mv command to rename it:

jobs:
  docit:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@master
    - name: rename war
      run: |
        mv "${dirname}/original.war" "${dirname}/newName.war"
JRichardsz
  • 14,356
  • 6
  • 59
  • 94
  • This given example has some issues. Using `clean package .. verify` run a lot of phases twice.. it's sufficient `mvn clean verify`... because `package` is part of the life cycle (https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html) – khmarbaise Aug 28 '21 at 09:15
  • The maven-jar-plugin does not support a property `jar.finalName`(https://maven.apache.org/plugins/maven-jar-plugin/jar-mojo.html) apart from that it would only change the name in `target` directory but not if do `mvn install` or `mvn deploy`.. – khmarbaise Aug 28 '21 at 09:16
  • another thing to mention km is that I was able to do version:set -DnewVersion command during the deploy line of my github actions however, that only put a datetime at the end of the jar/war name – 4help2 Aug 28 '21 at 12:21
  • I tried with a real maven project and worked. Attach your pom.xml to the question – JRichardsz Aug 29 '21 at 05:13
  • JRichardsz what did you do to make it work? I need it to be done using git actions however, was their something that you added to your pom file to make it work cause I could change my pom file around, I'll attach the build tag section of my pom file to the question – 4help2 Aug 29 '21 at 13:20