Monday, December 27, 2010

How To Create An Empty Java Project With Maven

To create an empty maven-managed java project simply type:

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart

You will be asked for the groupId and artifactId for your project, the project version and the main package for your code.

$ mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart
[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
Define value for property 'groupId': : cloudobjects
Define value for property 'artifactId': : demoProject
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  cloudobjects: : de.cloudobjects
Confirm properties configuration:
groupId: cloudobjects
artifactId: demoProject
version: 1.0-SNAPSHOT
package: de.cloudobjects
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: cloudobjects
[INFO] Parameter: packageName, Value: de.cloudobjects
[INFO] Parameter: package, Value: de.cloudobjects
[INFO] Parameter: artifactId, Value: demoProject
[INFO] Parameter: basedir, Value: /Users/helmut
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] project created from Old (1.x) Archetype in dir: /Users/helmut/demoProject
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.293s
[INFO] Finished at: Mon Dec 27 15:08:58 CET 2010
[INFO] Final Memory: 9M/81M
[INFO] ------------------------------------------------------------------------


Now you have a simple maven-managed java project.

If you are using eclipse you have (at least) two options:

To create an eclipse project from your maven project cd into the project-directory and type:

$ mvn eclipse:eclipse

If you are using the M2Eclipse-Plugin, you can import the maven project directly into eclipse.

To import a maven project into eclipse choose "File -> Import" and select "Maven -> Existing Maven Projects":


In the next step select the newly created project folder and the projects pom.xml:


Now you have an eclipse-project with full maven dependency management.

More information about the M2Eclipse-Plugin can be found in the sonatype M2Eclipse manual.

Tuesday, December 7, 2010

Fixing Ruby Gems On Mac OS Snow Leopard

Today I tried to install the ruby client library for cassandra. The gem install command failed with the following error message:

helmut$ sudo gem install cassandra
ERROR:  could not find gem cassandra locally or in a repository 

Running the same command with the verbose flag produced the following output:

helmut$ sudo gem install cassandra -V
HEAD 302 Found: http://gems.rubyforge.org/latest_specs.4.8
Error fetching remote data:  bad response Found 302 (http://gems.rubyforge.org/latest_specs.4.8)
Falling back to local-only install
ERROR:  could not find gem cassandra locally or in a repository


This output shows that the gem command tries to download the file http://gems.rubyforge.org/latest_specs.4.8 but cannot find it. In the next step it tried to download this file with curl. This worked. Downloading the file with the verbose flag produced the following output:

helmut$ curl -v http://rubygems.org/latest_specs.4.8
* About to connect() to rubygems.org port 80 (#0)
*   Trying 72.4.120.124... connected
* Connected to rubygems.org (72.4.120.124) port 80 (#0)
> GET /latest_specs.4.8 HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3
> Host: rubygems.org
> Accept: */*
> 
< HTTP/1.1 302 Found
< Date: Tue, 07 Dec 2010 08:18:46 GMT
< Server: Apache/2.2.3 (Red Hat) mod_ssl/2.2.3 OpenSSL/0.9.8e-fips-rhel5 Phusion_Passenger/3.0.0
< X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.0
< X-UA-Compatible: IE=Edge,chrome=1
< X-Runtime: 0.001094
< Location: http://production.s3.rubygems.org/latest_specs.4.8
< Content-Length: 0
< Status: 302
< Vary: Accept-Encoding
< Content-Type: text/html
< 
* Connection #0 to host rubygems.org left intact
* Closing connection #0
This output shows, that the request was redirected to http://production.s3.rubygems.org/latest_specs.4.8. Obviously the location of the ruby gems repository has changed. To fix the above problem, add the new repository url and remove the old one:
helmut$ sudo gem sources -a http://production.s3.rubygems.org/
helmut$ sudo gem sources -r http://gems.rubyforge.org/
After this fix the installation works without errors:
helmut$ sudo gem install cassandra
Successfully installed cassandra-0.8.2
1 gem installed
Installing ri documentation for cassandra-0.8.2...
Installing RDoc documentation for cassandra-0.8.2...