ICE09 . playing with java, scala, groovy and spring .

Playing with Spring

Archive for December, 2010

QuickTip: SCP with Groovy, Maven and Eclipse

Posted by ice09 on December 20, 2010

Intention

It’s easy to use SCP with Java, Groovy and even with Maven. However, it gets more complicated if you want to use all of them simultaneously.

It should work this way: An (key-file-based) SCP-connection should take place in a Groovy, so the transferred file can be manipulated straight away. All this should be triggered by a Maven build process and alternatively, using Eclipse with the Groovy plugin, it should be possible to directly start the Groovy script.

Preparation

Either Eclipse with the Groovy plugin and the Maven plugin or the SpringSource Tool Suite (which includes both plugins) should be installed.

Maven config

The Maven config consists of two dependencies:

  1. The Ant-SCP-Plugin, which is based on JSch
  2. Embedded Groovy (groovy-all.jar)

And two Maven Plugins:

  1. GMaven
  2. The Exec Maven Plugin

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>de.ice09.scp</groupId>
 <artifactId>jsch</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <dependencies>
  <dependency>
   <groupId>org.apache.ant</groupId>
   <artifactId>ant-jsch</artifactId>
   <version>1.8.1</version>
  </dependency>
  <dependency>
   <groupId>org.codehaus.groovy</groupId>
   <artifactId>groovy-all</artifactId>
   <version>1.7.5</version>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <plugin>
    <groupId>org.codehaus.groovy.maven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <executions>
     <execution>
      <goals>
       <goal>generateStubs</goal>
       <goal>compile</goal>
       <goal>generateTestStubs</goal>
       <goal>testCompile</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
     <execution>
      <goals>
       <goal>java</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <mainClass>de.ice09.scp.LogLoader</mainClass>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

Groovy…

Here comes the Groovy part, in fact it’s quite short, since it focuses on the file transfer part. Obviously, it would be easy to manipulate the transferred files after the SCP-transfer, since we are inside a Groovy script and we can use whatever Java library we want since we are inside a Maven project. The universe is ours!


package de.ice09.scp

new AntBuilder()
 .scp(
  file:"root@server.com:/tmp/ls",
  todir:'C:/temp',
  verbose:true,
  trust:true,
  keyfile:'C:/portables/PuTTY/ossh.ppk',
  passphrase:'passphrase'
 )

The file must go to src/main/groovy for GMaven to work. The key must be OpenSSH-compliant. If you use PuTTY, you can export this format. Alternatively, you can use username/password instead.

Testing…

After creating a new Maven project with the pom.xml listed above, more than half of the work is done. With or without an IDE this can be tested. Just run

maven clean install exec:java

GMaven should generate the Java stubs, the Execution Plugin should execute the generated Java class file. The dependencies for JSch, Antbuilder etc. are handled by Maven or Groovy itself.

Eclipse config

The final step is to enable the Groovy nature in Eclipse. This leads us to a faster development cycle, since the Groovy script can be run “natively”, without having Maven to generate the Java stub before. There is no problem running the two options (Groovy with Maven and Eclipse) side-by-side, so it is really easy to develop and deploy, since there is no big difference. You always have a completely runnable version available, given that Maven is installed on the target system.

Convert to Groovy project

2010-12-20_23-16_Java

Add Groovy sources (from src/main/groovy) to classpath

2010-12-20_23-19_Java

The deployable way: start process using GMaven and Execution Plugin

2010-12-20_23-36_Run Configurations

Start in development mode using the Groovy Eclipse Plugin

2010-12-20_23-38_Java

Posted in Groovy, Maven | 1 Comment »

Preparing a fresh Debian Lenny for Java Enterprise Applications

Posted by ice09 on December 18, 2010

Intention

I am a happy owner of a virtual root server – this being a great thing, there is one big problem: I have to prepare this box for Java usage myself. This must be achieved with a somewhat limited Linux knowledge. However, with debian lenny this turned out to be a breeze.

I had to follow the steps below to get this running in about 30 minutes:

  1. Apache Webserver
  2. MySQL
  3. phpMyAdmin
  4. GlassFish 3

Which is pretty much what you need for creating JEE/Spring-Applications in an enterprise scale.

What is still missing (due to a lack of time and necessity) are the following configurations:

  1. JNDI-Resource for the MySQL-Database
  2. Apache/GlassFish-mod for forwarding requests on port 80 to GlassFish directly

List of Commands

cat /etc/debian_version

For my installation this says 5.0.7. So obviously: 1. I am using Debian. 2. It is Debian Lenny in version 5.0.7. Yes, we are starting slowly here.

apt-get install apache2

Install the Apache Web Server. I more or less need it for phpMyAdmin. Obviously, you can choose lighthttpd or others here.

apt-get install mysql-server mysql-client phpmyadmin

This is the most important line, it magically does everything exactly like you want to have it (however, this depends on answering the questions during installation correctly).

apt-get install sun-java6-jdk

Ok, this is important as well for Java development. I did not expect it to be that easy (remembering licensing issues in the past) and tried it “manually”, but was lost almost immediately. However, with sun-java6-jdk, you will get everything you need. You should check this with:

java –version

which should print out text like this:

java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03, mixed mode)

Your mileage may vary here of course. It should show 1.6 somewhere though.

Now, the most “difficult” part is the installation of GlassFish. Not surprisingly, we are not the first trying to install GlassFish on Debian, so there exists a really good documentation about this topic here (in German): http://www.bennyn.de/webanwendungen/virtual-server/glassfish-v3-installieren-auf-debian-lenny.html

Since this HOWTO here should be complete, I am reposting the steps here:

wget -O /opt/glassfish-3.0.1.zip http://download.java.net/glassfish/3.0.1/release/glassfish-3.0.1.zip
cd /opt/
unzip glassfish-3.0.1.zip
rm glassfish-3.0.1.zip
cd /etc/init.d/

Now, only the script glassfish.sh is missing:

vim /etc/init.d/glassfish.sh
touch glassfish.sh
chmod 0755 glassfish.sh    
update-rc.d glassfish.sh defaults

with this content (all taken from the site mentioned above):

#!/bin/sh
### BEGIN INIT INFO
# Provides:          glassfish
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO
#
# This init.d script is used to start the glassfish application server.
# Simplest possible case -- no password file, one default domain
#
 
GLASSFISH_HOME=${GLASSFISH_HOME:-"/opt/glassfishv3/glassfish"}
 
case "$1" in
start)
	$GLASSFISH_HOME/bin/asadmin start-domain | tee -a /var/log/glassfish.log
	;;
stop)
	$GLASSFISH_HOME/bin/asadmin stop-domain | tee -a /var/log/glassfish.log
	;;
restart)
	$GLASSFISH_HOME/bin/asadmin restart-domain | tee -a /var/log/glassfish.log
	;;
*)
    echo "usage: $0 (start|stop|restart|help)"
esac

Posted in Debian, GlassFish | 2 Comments »