March 10, 2015

Maven: Copy file to Linux from Windows and Execute Remote Bash file

I have been working on a project that requires to run on a remote Linux machine.
Every time I make changes, I build the jar -> sign it -> rename the jar -> copy to Linux Server -> run Bash file.

It was exhausting steps, so I have search for I way that Maven can handle all these steps.
What I have are the followings:

1. Add custom build number


1
2
3
4
5
<properties>
  <build.version>2.1.90</build.version>
  <timestamp>${maven.build.timestamp}</timestamp>
  <maven.build.timestamp.format>HH:mm dd.MM.yyyy</maven.build.timestamp.format>
</properties>

2. Building a Jar file contains all dependencies


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<artifactId>maven-assembly-plugin</artifactId>
  <configuration>
   <finalName>jarfileName-${build.version}</finalName>
   <archive>
     <manifest>
       <addClasspath>false</addClasspath>
       <mainClass>sawalha.MainClassName</mainClass>
     </manifest>
     <manifestEntries>
       <Implementation-Version>${build.version}</Implementation-Version>
       <Permissions>all-permissions</Permissions>
       <Codebase>*</Codebase>
       <Implementation-Build>${project.version}</Implementation-Build>
       <Build-Date>${maven.build.timestamp}</Build-Date>
     </manifestEntries>
   </archive>
   <descriptorRefs>
     <descriptorRef>jar-with-dependencies</descriptorRef>
   </descriptorRefs>
   <appendAssemblyId>false</appendAssemblyId>
   <finalName>jarfileName-${build.version}-dep</finalName>
  </configuration>
  <executions>
   <execution>
     <id>make-assembly</id>
     <phase>package</phase>
     <goals>
       <goal>single</goal>
     </goals>
   </execution>
  </executions>
</plugin>

3. Sign the Jar file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jarsigner-plugin</artifactId>
  <version>1.2</version>
  <executions>
    <execution>
      <id>sign</id>
      <goals>
        <goal>sign</goal>
      </goals>
    </execution>
    <execution>
      <id>verify</id>
      <goals>
        <goal>verify</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <keystore>sign-dir\file.p12</keystore>
    <alias>"signature alias"</alias>
    <storepass>storepass</storepass>
    <keypass>keypass</keypass>
    <storetype>pkcs12</storetype>
  </configuration>
</plugin>

4. Setup Apache Ant

    1. Download and Install ANT
    2. Download the optional Ant libraries for FTP (ant-commons-net.jar & commons-net-3.3.jar)
    3. Download the optional Ant libraries for SSH (ant-jsch-1.8.1.jar & jsch-0.1.44.jar)
    4. Create directory under the maven project (I called it ant-lib)

5. Copy the Jar file into Linux machine & Execute the Bash file


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.5</version>
  <configuration>
    <target>
      <echo message="hello ant, from Maven!" />
      <echo>Maybe this will work?</echo>
      <taskdef name="ftp"
        classname="org.apache.tools.ant.taskdefs.optional.net.FTP">
        <classpath>
          <pathelement
            location="/ant-lib/ant-commons-net.jar" />
          <pathelement
            location="/ant-lib/commons-net-3.3.jar" />
        </classpath>
      </taskdef>
      <taskdef name="sshexec"
        classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec">
        <classpath>
          <pathelement
            location="/ant-lib/ant-jsch-1.8.1.jar" />
          <pathelement
            location="/ant-lib/jsch-0.1.44.jar" />
        </classpath>
      </taskdef>
      <ftp action="send" server="10.20.30.40" remotedir="/deployment/java"
        userid="userName" password="password" dir="${project.build.directory}">
          <include name="jarfileName-${build.version}-dep.jar" />
        </fileset>
      </ftp>
      <sshexec host="10.20.30.40" username="userName" password="password"
        command="/home/scripts/update.fp.sh ${build.version}"
        trust="true" />
    </target>
  </configuration>
</plugin>

To execute the goal, run the following command:
>mvn antrun:run

Waiting for your questions... Have a nice day.

No comments: