Archive for the ‘Java’ Category

Maven Commands

Friday, February 20th, 2009

A list of the maven commands and references I commonly use.

Creating Maven Projects

  • mvn archetype:create -DgroupId=com._3kbo -DartifactId=application (create a project for building a jar file )
  • mvn archetype:create -DgroupId=com._3kbo -DartifactId=webapplication -DarchetypeArtifactId=maven-archetype-webapp (create a web app project for building a war file)
  • mvn archetype:generate (Select the number of the desired archetype.)

Building, Testing, Packaging and Installing to the Repository

  • mvn clean            (cleans the project, i.e. delete the target directory)
  • mvn compile        (compiles the source code)
  • mvn test              (if required compiles the source code, then runs the unit tests)
  • mvn package       (compiles, tests then packages the jar or war file)
  • mvn clean install  (cleans the project, compiles, tests, packages and installs the jar or war file)
  • mvn install           (compiles, tests, packages and installs the jar or war file)
  • mvn -o install      (compiles, tests, packages and installs the jar or war file in offline mode)
  • mvn -Dmaven.test.skip package       (package without running the tests)
  • mvn -Dmaven.test.skip clean install  (clean and install without running tests)
  • mvn -o test         (test offline)

Unit Tests

  • mvn test –Dtest=AspectTest (run a specific test)
  • mvn test -Dtest=*ModelTest (run a subset of tests using regular expression. E.g. test all ModelTest classes)

Excluding log4j.properties from jar file

Move log4j.properties from src/main/resources to src/test/resources.
That way it gets picked up by the testResources for testing but excluded from the jar file.

Manually Install a file to the Repository

mvn install:install-file -DgroupId=com._3kbo -DartifactId=model -Dversion=1.0.0 -Dpackaging=jar -Dfile=new_file.jar

Set Java Build Version

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>

Documentation

The mvn site command builds a useful dependency tree at target/site/dependencies.html.

  • mvn site (generate documentation in the target/site directory)
  • mvn javadoc:javadoc (generate javadoc in target/site/apidocs directory )
  • mvn help:active-profiles (lists the profiles currently active)
  • mvn help:effective-pom (displays the effective POM for the current build)
  • mvn help:effective-settings (displays the calculated settings for the project, given any profile enhancement and the inheritance of the global settings into the user-level settings.)
  • mvn help:describe (describes the attributes of a plugin)

For large projects mvn site can run out of memory. Use MAVEN_OPTS in the mvn or mvn.bat script to allocate additional memory, e.g:

MAVEN_OPTS=”-Xmx512m -Xms256m -XX:MaxPermSize=128m”

Repositories

Jena http://jena.hpl.hp.com/repo
Jersey http://download.java.net/maven/2/
Maven Central http://www.ibiblio.org/maven2

References

Maven Home
Maven: The Definitive Guide
Better Builds with Maven
Maven Lifecycle

Setting up MySql on Mac OSX for Jena SDB

Friday, October 17th, 2008

Awhile ago I installed MySQL 5.0.37 on my MacBook Pro using the default mysql settings.

Recently I installed Jena SDB 1.1, following the instructions on the wiki.

As part of the install I created a mysql database, specifying utf8, e.g.

mysql> create database sdb-index character set utf8 ;

and set up a store description (named sdb-index.ttl) based on the SDB example, changing it to use mysql and the “layout2/index” layout.

The create command worked fine
SDBROOT > bin/sdbconfig –sdb=sdb-index.ttl –create

but when I ran the testsuite
SDBROOT > bin/sdbtest –sdb=sdb-index.ttl testing/manifest-sdb.ttl

I got the following error in the Unicode-5 test.

Checking out the SDB notes for Mysql it seemed likely that the problem was related to the msyql default character set.

To see what was currently set I ran the “show variables” command below

mysql> show variables like ‘character%’;
+————————–+————————————————————+
| Variable_name | Value |
+————————–+————————————————————+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.0.37-osx10.4-i686/share/mysql/charsets/ |
+————————–+————————————————————+

The SDB notes for Mysql recommends setting default-character-set=utf8. The Mysql documentation seemed to favour setting character-set-server=utf8 and collation-server=utf8_general_ci.

To make the changes I needed to create a config file with the changed settings that mysql reads on startup.

To do this I copied the example config file for small installations to /etc/my.cnf.

cp /usr/local/mysql-5.0.37-osx10.4-i686/support-files/my-small.cnf /etc/my.cnf

In the [mysqld] section of my.cnf I added the lines:
# utf8
init-connect=’SET NAMES utf8′
character-set-server=utf8
collation-server=utf8_general_ci

After restarting mysql the “show variables” command showed the following utf8 updates.
mysql> show variables like ‘character%’;
+————————–+————————————————————+
| Variable_name | Value |
+————————–+————————————————————+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.0.37-osx10.4-i686/share/mysql/charsets/ |
+————————–+————————————————————+

When I tried it again the SDB testsuite ran without errors.

SDBROOT > bin/sdbtest –sdb=sdb-index.ttl testing/manifest-sdb.ttl