0

I have a Maven 3 job configured in my Hudson server.

When the Hudson server successfully builds the Maven jar, I have the jar being deployed to an SCP repository on another server.

I want to execute a relink.sh script on the remote host where one of the parameters is the name of the newly delivered jar file (e.g. /usr/bin/relink.sh myproject-0.0.2-SNAPSHOT.jar).

How do I get that Maven-based file / version name into a variable that I can then use in my Hudson call to execute the remote script?

Lorin S.
  • 95
  • 2
  • 10

3 Answers3

0

I'm not sure if you can or can't pass the variable, but a work-around would be to first copy the file to a staging directory, instead of directly to where you want it to go. Then have your script look in the staging directory for a file and move it:

STAGE_DIR=/path/to/stage
DEST_DIR=/path/to/dest
LINK=/path/to/link.jar

while read file; do
    mv $file $DEST_DIR
    ln -s -f $DEST_DIR/$(basename $file) $LINK
done < <( find $STAGE_DIR -name "myproject*SNAPSHOT.jar" )

You might want to do some error checking and exit with non-0 if there is, e.g., more than one file that matches, no files, etc. Have you solved the issue?

zerodiff
  • 101
  • 2
0

Like @zerodiff suggested there seems to be a number of ways to do this via Hudson but they all seemed to be brittle and difficult to maintain when managing a large number of projects. I instead used Maven's wagon plugin to implement the deployment via Maven instead of via Hudson.

The configuration I used in the maven projects:

<properties>
    <!-- Deploy -->
    <dev.server.hostname>192.168.1.1</dev.server.hostname>
    <application.name>test-application</application.name>
    <!-- Deploy -->
</properties>

<build>
    <extensions>
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ssh</artifactId>
            <version>1.0</version>
        </extension>
    </extensions>
</build>

<profiles>
    <profile>
        <id>hudson</id>
        <activation>
            <property>
                <name>env</name>
                <value>hudson</value>
            </property>
        </activation>
        <build>
            <filters>
                <filter>src/main/filters/${env}-linux.properties</filter>
                <filter>src/main/filters/${env}.properties</filter>
            </filters>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>wagon-maven-plugin</artifactId>
                    <version>1.0-beta-5</version>
                    <executions>
                        <execution>
                            <id>dev-deploy</id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>upload</goal>
                            </goals>
                            <configuration>
                                <fromDir>${project.build.directory}</fromDir>
                                <includes>${project.build.finalName}.jar</includes>
                                <url>scp://${dev.server.hostname}/</url>
                                <toDir>/opt/${application.name}</toDir>
                                <serverId>dev-deploy-server</serverId>
                            </configuration>
                        </execution>
                        <execution>
                            <id>execute-dev-deploy-commands</id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>sshexec</goal>
                            </goals>
                            <configuration>
                                <url>scp://${dev.server.hostname}/</url>
                                <serverId>dev-deploy-server</serverId>
                                <commands>
                                    <command>sudo /etc/init.d/${application.name} stop</command>
                                    <command>sleep 2</command>
                                    <command>sudo /opt/${application.name}/relink.sh ${project.build.finalName}.jar</command>
                                    <command>sudo /etc/init.d/${application.name} start</command>
                                </commands>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

In my maven settings.xml, I have the credentials for the dev-deploy-server:

    <server>
        <id>dev-deploy-server</id>
        <username>hudson_bot</username>
    </server>

With the Maven plugin in Hudson, I added the "Invoke Maven 3" Build directive and:

  • Goals: clean deploy
  • Properties: env=hudson

Finally I set up the security between the two servers so that "hudson_bot" could scp to the dev-deploy-server and gave hudson_bot sudoer NO_PASSWD privileges to the two mentioned scripts in the ssh execute element.

Lorin S.
  • 95
  • 2
  • 10
0

You want to get the jar artifact name from Maven itself in order to prevent adjusting your script when developers change the artifact's id or version. Scanning for *.jar files is not elegant as it would match more than the main artifact (e.g. *-javadoc.jar). So to get the main artifact version from Maven I rely on 2 facts:

  • by default, the jar plugin uses the artifact's id and version for the final jar artifact, e.g. target/the-service-0.0.1-SNAPSHOT.jar
  • the artifact's id, group and version are stored in pom.properties

    $ cat target/maven-archiver/pom.properties
    #Generated by Apache Maven
    #Fri Jul 06 15:10:03 CEST 2018
    version=0.0.1-SNAPSHOT
    groupId=net.company
    artifactId=the-service
    

Note that java devs can still change the default build folder target and break your hudson job.

Alex
  • 101
  • 3