Showing posts with label maven. Show all posts
Showing posts with label maven. Show all posts

Friday, September 7, 2012

How to fix error “Updating Maven Project”. Unsupported IClasspathEntry kind=4?

Reposting an answer to this problem.
Disable the maven nature for the project (via the right-click menu), run mvn eclipse:clean (while your project is open in STS/eclipse), and then re-enable the maven nature. 
http://stackoverflow.com/questions/10564684/how-to-fix-error-updating-maven-project-unsupported-iclasspathentry-kind-4

Monday, June 4, 2012

Referencing third-party library source code in a GWT Project

Found a great blog post on how to use 3rd party source code in a GWT project.

The only thing that I will add is that you will also need need to configure just your projects module to be loaded, otherwise you will get an error about gwt.xml files not having an entry point.

...
<artifactId>maven-source-plugin</artifactId>
...
<configuration>
...
<modules>
<module>com.my.Module</module>
</modules>
...
</configuration>
...

Friday, May 11, 2012

GWT + Maven + Eclipse = Pain

GWT was definitely not designed with Maven in mind, and getting it all to work within Eclipse is a nightmare. I have managed to get this mostly working, here is what I have so far.

Please install Eclipse with the m2eclipse and GWT plugins.

Start by using the GWT archetype, sadly, this will not produce a project that works, but at least gets you pointed in the right direction.

mvn archetype:generate \
 -DarchetypeRepository=repo1.maven.org \
 -DarchetypeGroupId=org.codehaus.mojo \
 -DarchetypeArtifactId=gwt-maven-plugin \
 -DarchetypeVersion=2.4.0

This will request some details, including a module name. Go ahead and enter in all the fields, but the first thing you will need to do is a search on the entire project in eclipse for literally ${module} and replace that with the actual name of your new module. You should at least find this problem in the org.eclipse.wst.common.component xml file.

You will also notice that the pom.xml file has some compilation issues. I have not figured out how to resolve other than commenting out the offending lines. This doesn't seem to hurt anything, but you will need to manually generate the Async interfaces.

Next up, you will need to create a Run Configuration, most easily done by right clicking your project -> run as -> (G) Web Application. This will throw some error about needing to include the module name in the arguments. So go find the Run Configuration that it created and tack on the end the full path to your endpoint (eg. com.myapp.MyEndPoint)

Now you should be able to get the server standing by following the run as step above again.

You will notice that there are still some funky warnings being spat out, which I have not totally figured out yet, but hopefully this will help someone out.

Thursday, May 10, 2012

m2eclipse: Add archetypes to create maven project

Ran into an issue where I could not use an archetype from within Eclipse. In order to do so, a new catalog will need to be created, then Eclipse simply needs to be told where it is.
  • Navigate to your .m2/repository in your console
  • Enter the command 'mvn archetype:crawl'
This will create the file archetype-catalog.xml

In Eclipse create a new Maven project, when you get to the screen with the catalog drop down box, click configure and add a Local Catalog pointing to the xml file created above. Ok out.

This catalog will now be added to the drop down box and can be selected.

Enjoy.

Thursday, January 19, 2012

Maven Test Dependency

With the maven-jar-plugin's goal test-jar, it is possible to create testing dependencies.

When doing this it is important to include the dependency twice, once normally (main code) and again with <type>test-jar<type>. So long as the dependency is built first, maven will find the test code.

Note: Eclipse doesn't seem to care which if both dependencies are there, causing some confusion, but when you go to run mvn from the cli, it will care.

http://maven.apache.org/guides/mini/guide-attached-tests.html

Friday, April 1, 2011

How to create a Self-Executing Jar with Maven

Example
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.1</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>namespace.ClassWthMainMethod</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>

Maven Assembly Plugin Example

Example
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2.1</version>
        <executions>
          <execution>
            <id>create-target</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
             <descriptors>
               <descriptor>path/to/assembly.xml</descriptor>
             </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>

See Also:

Sunday, March 27, 2011

Sample Java usage for MongoDB

Maven
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.5</version>
</dependency>

Connect to Mongo
import com.mongodb.DB;
import com.mongodb.Mongo;

//...

Mongo mongo = new Mongo(host, 27017);
DB db = mongo.getDB("name of db"); //If db does not exist, then it will be created.

Insert JSON Data
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;

//...

DBCollection coll = db.getCollection("testCollection"); //If collection does not exist, then it will be created.
BasicDBObject doc = new BasicDBObject();
doc.put("hello", "world");

BasicDBObject inner = new BasicDBObject();
inner.put("key","value");
doc.put("inner", inner);

coll.insert(doc);

Fetch JSON Data
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection
import com.mongodb.DBCursor;

//...

DBCollection coll = db.getCollection("testCollection");
BasicDBObject query = new BasicDBObject();
query.put("hello", "world");
DBCursor cursor = coll.find(query);
while(cursor.hasNext()) {
System.out.println(cursor.next());
}

GridFS
import com.mongodb.gridfs.GridFS;

//...

GridFS fs = new GridFS(db, "collection"); //If bucket does not exist, it will be created.

GridFS Save File
import java.io.File;
import com.mongodb.gridfs.GridFSInputFile;

//...

GridFSInputFile file = fs.createFile(new File("/path/to/file.ext"));
file.save();

GridFS List Files
import com.mongodb.DBCursor;

//...

DBCursor cursor = fs.getFileList();
while(cursor.hasNext()) {
System.out.println(cursor.next());
}

GridFS Read File
GridFSDBFile file = fs.findOne("file.ext");
BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()));
String line = null;
while((line = reader.readLine()) != null){
System.out.println(line);
}

Map/Reduce
Example Dataset:
{ "_id" : ObjectId("4d8233d6d638a2ca105b1fab"), "name" : "John", "number" : 2 }
{ "_id" : ObjectId("4d8233d6d638a2ca105b1fac"), "name" : "Jane", "number" : 2 }
{ "_id" : ObjectId("4d8233dbd638e40a183d27f8"), "name" : "John", "number" : 2 }
{ "_id" : ObjectId("4d8233dcd638e40a183d27f9"), "name" : "Jane", "number" : 2 }

Code Example:
import com.mongodb.MapReduceCommand;
import com.mongodb.MapReduceOutput;

//...

String map = "function(){" +
"emit(this.name, {count: 1, sum: this.number});" +
"};";

String reduce = "function( key , values ){" +

"var n = { count: 0, sum: 0}; " +

"for ( var i = 0; i < values.length; i ++ ) {" +

"n.sum += values[i].sum;" +

"n.count += values[i].count;" +

"};" +

"return n;" +

"};";

MapReduceOutput out = coll.mapReduce(map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
for ( DBObject obj : out.results() ) {
System.out.println( obj );
}

Expected Output:
{ "_id" : "Jane" , "value" : { "count" : 2.0 , "sum" : 4.0}}
{ "_id" : "John" , "value" : { "count" : 2.0 , "sum" : 4.0}}

Saturday, March 26, 2011

How to build a basic Pax-Runner application with Maven that utilizes spring-dm

My goal is to create a Maven based solution that can automatically assemble a pax-runner driven application that is ready to be deployed.

To make this work with a larger application, there should be an outer pom that includes this and any other bundles as modules. This should be the last module processed, the assembly process will pick up on the other modules built at the same time.


Directory Structure:
  • pom.xml
  • src
    • main
      • etc
        • assembly
          • paxrunner-assembly.xml
        • scripts
          • run.bat - for windows
          • run.sh - for *nix

To start I created a Maven pom file that has pax-runner as a dependency and references a custom assembly file.

pom.xml
<dependencies>
<dependency>
<groupId>org.ops4j.pax.runner</groupId>
<artifactId>pax-runner</artifactId>
<version>${pax-runner.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>create-target</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/etc/assembly/paxrunner-assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

The assembly file will tell Maven how to package the final output. The xml is fairly easy to read, see the maven-assembly-plugin documentation for specifics. The one thing worth calling out is the exclusion of the spring and related libraries, this is because pax-runner already has a built in profile that will download and wire them up properly.

paxrunner-assembly.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

<id>distribution</id>

<formats>
<format>dir</format>
<format>zip</format>
</formats>

<includeBaseDirectory>false</includeBaseDirectory>

<files>
<file>
<source>src/main/etc/scripts/run.sh</source>
<fileMode>0777</fileMode>
</file>
<file>
<source>src/main/etc/scripts/run.bat</source>
<fileMode>0777</fileMode>
</file>
</files>

<moduleSets>
<moduleSet>
<binaries>
<unpack>false</unpack>
<includeDependencies>true</includeDependencies>
<outputDirectory>lib/module-bundles</outputDirectory>
<dependencySets>
<!-- 3rd party libraries -->
<dependencySet>
<outputDirectory>lib/required-bundles</outputDirectory>
<excludes>
<exclude>org.springframework:org.springframework.core</exclude>
<exclude>org.springframework:org.springframework.aop</exclude>
<exclude>org.springframework:org.springframework.asm</exclude>
<exclude>org.springframework:org.springframework.beans</exclude>
<exclude>org.springframework:org.springframework.context</exclude>
<exclude>org.springframework:org.springframework.context.support</exclude>
<exclude>org.springframework:org.springframework.expression</exclude>
<exclude>org.slf4j:*</exclude>
<exclude>org.aopalliance:*</exclude>
<exclude>org.apache.commons:*.logging</exclude>
</excludes>
</dependencySet>
</dependencySets>
</binaries>
</moduleSet>
</moduleSets>

<dependencySets>
<!-- Adds pax-runner to root of distribution -->
<dependencySet>
<scope>provided</scope>
<includes>
<include>org.ops4j.pax.runner:pax-runner</include>
</includes>
<outputFileNameMapping>${artifact.artifactId}.${artifact.extension}</outputFileNameMapping>
</dependencySet>
</dependencySets>

</assembly>

Finally for the scripts, these do nothing more than start up pax-runner and pass in any directories of bundles or configuration files.


run.sh
java -jar pax-runner.jar --profiles=spring.dm --workingDirectory=. scan-dir:lib/required-bundles scan-dir:lib/module-bundles

run.bat
@echo off
call java -jar pax-runner.jar --profiles=spring.dm --workingDirectory=. scan-dir:lib/required-bundles scan-dir:lib/module-bundles %1 %2 %3