miércoles, 18 de diciembre de 2013

Crear uberjars con maven 2

Es sabido que maven 3 tiene plugins más prácticos al respecto. Maven 2 nos permite hacerlo con el assembly plugin    
       <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>package.MainApp</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

viernes, 17 de mayo de 2013

Carbon 5 Database Migration: a Maven plugin for keeping your development and production databases up-to-date

So, you just added a new column to the database schema to giving support to a new feature, but the code crashes in production because someone forgot to update the database schema?
The Carbon 5 Database Migration Plugin aims to solve this. Each time you update your code and need to update your database you just ask Maven to do it.
It's done quite easily: the DB schema syncronization are stored as incremental modifications in the schema into separated .sql files with a timestamp in its filename and c5-database-migration takes care of them.
To set up Carbon 5 db migration you need to:
Add the following plugin repository in your pom.xml
<pluginRepositories>
    <pluginRepository>
        <id>c5-public-repository</id>
        <url>http://mvn.carbonfive.com/public</url>
    </pluginRepository>

</pluginRepositories>

Also, add the folloing plugin in the pom.xml
<plugin>
    <groupId>com.carbonfive.db-support</groupId>
    <artifactId>db-migration-maven-plugin</artifactId>
    <configuration>
        <url>jdbc:mysql://localhost:3306/databasename</url>
        <username>root</username>
        <databaseType>MYSQL</databaseType>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
    </dependencies>
 </plugin>
Pay attention that this one is a set-up for MySQL.
Each time you need to modify your DB schema you just run the following maven command within your project's directory:
mvn db-migration:new
and then you wirte down your SQL statements to update the schema in there. To make the plugin to update your schema you need to run the following command:
mvn db-migration:migrate
If it finds that the DB is outdated it will run the necesary updates. If the DB is updated it will do nothing.
There's another goals for creating and dropping databases, but I will not cover them since they are quite tricky. The main issue with database creation and dropping is that quite often we rely on DB servers which we don't have a sufficiently privileged accounts to create new databases. So, if you need to reset your database you'll have some issues. You can work it around droping manually your database and then creating a blank schema again, or maybe modifying your local pom.xml to have the root access (or a sufficiently privileged one) to your local database.
More info in the following link:
http://code.google.com/p/c5-db-migration/wiki/MavenPlugin

jueves, 16 de mayo de 2013

m2e-wtp: Get rid of that ugly eclipse:eclipse goal in your maven web perojects.

So, are you tired to going to your command line to run a mvn eclipse:eclipse each time you try a new dependency in your pom.xml?
You just have to install the m2eclipse plugin in your Eclipse, followed by the m2e-wtp plugin. With that you will no longer need to run the eclipse:eclipse goal to get your eclipse accepting your project as a web project. You will just have to import it as a Maven Project.
Just... don't forget to set the wtp plugin in your pom.xml:
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <configuration>
          <wtpapplicationxml>true</wtpapplicationxml>
          <wtpversion>2.0</wtpversion>
        </configuration>
      </plugin>

martes, 14 de mayo de 2013

Querying SOAP in Java with JAX-WS portable artifacts

Generating the artifacts
We need the WSDL file. Then we use a command called wsimport. With this command, we'll generate all needed classes to make queries to the service. Basically, the syntax we'll neded is the following:
wsimport -p <package> <wsdl_file_path>
For example:
wsimport -p stockquote http://stockquote.xyz/quote?wsdl
It generates the artifacts for the services described by the wsdl in the url (it can contain a hard disk path also) contained in the package "stockquote". For more information about wsimport, check the folloowing link:
http://docs.oracle.com/javase/6/docs/technotes/tools/share/wsimport.html
Making the query
The generated artifacts will contain a class named Service. That class will handle the connection and will contain the a methods supported by the service. So, you'll have to:
Establish the connection
Service svc = new Service(new URL(SOAP_WSDL_URL), new QName(SERVICE_URL, "Service");
ServiceSoap svcSOAP = svc.getServiceSoap();
Making the query
svcSOAP.<method>(<arguments>);
The generated artifacts will contain the classes needed to pass as parameters.

miércoles, 8 de mayo de 2013

Debugging Stand-alone Java applications

When developing applications running in standalone servers, we usually find ourselves guessing the nature of the bugs and exceptions we get. That's when we miss the debugger.
Although is well know that most standalone Java applications are debuggeable, when running outside the context of our IDE it seems quite mysterious the way to do it.
So, my coworker had spent some time researching the matter and told me the solution.
We were running a CQ5.5 WCM stand alone jar, and we wanted to debug the OSGi servlets we were developing for it (CQ has an Apache Felix OSGi container within).

java -Duser.country=AR -Duser.timezone=America/Argentina/Buenos_Aires -Dsun.lang.ClassLoader.allowArraySyntax=true -XX:MaxPermSize=256m -XX:+UseParallelGC -Xmn128m -Xms512m -Xmx1024m -Xss2m -agentlib:jdwp=transport=dt_socket,address=localhost:30303,server=y,suspend=n -jar cq55-author-4502.jar -gui -p 4502 -nofork

The most important parameters are those I put in bold.
-agentlib:jdwp=transport=dt_socket,address=localhost:30303,server=y,suspend=n
From this line we should look at "localhost:30303" which is the address/port where the JVM will be listening for debuging.

In the other hand we'll have to configure our IDE to debug that standalone server. I was using Eclipse for that. You'll have to get into the "Debug Configurations" dialog (just look at the debug button drop-down menu). There you'll have to take care for the following config parameters.
  • Name: some name for you to identify what are you going to debug when you press the Debug button in your Eclipse
  • Project: the project you want to debug
  • Host/Port: remember that string address=localhost:30303? Exactly!
  • Source tab: just check that your sources are there. If any source is missing just add it there.
 And that's all folks!

lunes, 12 de diciembre de 2011

Creando un nuevo proyecto Spring3 con Maven

Para crear el proyecto, escribir en consola:
 mvn archetype:generate -DgroupId=com.mkyong.core -DartifactId=Spring3Example -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Adentro se crea el directorio que contiene el proyecto y el pom.xml (que es el fichero que permite a maven configurar el proyecto)

El la sección 1.3.1.2 (Maven Dependency Management) de la documentación de Maven3 se aclara que hay que agregar esta dependencia a las dependencias del pom.xml (tag "dependencies"):

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.0.RELEASE</version>
<scope>runtime</scope>
</dependency>

Aunque si agregamos al comienzo del pom:

<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>

podemos reemplazar luego el tag "version" como sigue:

<version>${spring.version}</version>

Con ello podremos actualizar la versión de Spring cambiando solo una línea en el POM, en el caso de que tengamos múltiples dependencias.


jueves, 31 de marzo de 2011

MySQL Portable

In my work we have very strict security policies regarding software installation on our desktops. That's a real PITA when you really need to do some experimentation with configuration.
I found over there a way to get a MySQL server running in your system without having to use an installator.
You'll need to get a noinstall version of MySQL. You can find it here: http://dev.mysql.com/downloads/mysql/5.1.html

Then, you have to unpack it. You can do it into a directory like c:\MySQLServer. To start it up, you could use a script like this one:
@echo off

echo Launching mysqld listening on port 3306
bin\mysqld --user=root --datadir=data --port=3306 --basedir=.
This script must be in the same directory in which MySQL is.
Now, you just have to run the script.
I'd really prefer to use some other scripting language like bash, but this is windows...