Netbeans uses Ant – for Maven projects

10 mars 2010

A small think appeared to me today, regarding Netbeans.

I noticed that when I run a test for a single file (Right-click on the file, then Test):

The first time the test was running ok, but the second time, I had a strange error from Datanucleus, telling me my classes are wrong….

That was really annoying, so I checked a little bit further this and set debugging option to Maven and to Ant in the settings dialog.

The first time I test the file, I get this:

1
2
3
4
NetBeans: Executing '/mnt/local/files/apache-maven-2.2.1/bin/mvn -Dtest=BasicTest -Dnetbeans.execution=true -debug test-compile surefire:test'
NetBeans:      JAVA_HOME=/mnt/local/files/jdk16+ Error stacktraces are turned on.Apache Maven 2.2.1 (r801777; 2009-08-06 21:16:01+0200)Java version: 1.6.0_17Java home: /mnt/local/files/jdk1.6.0_17/jreDefault locale: fr_FR, platform encoding: UTF-8OS name: "linux" version: "2.6.32.8-1-jolicloud" arch: "i386" Family: "unix"
[DEBUG]Building Maven user-level plugin registry from: '/home/gerard/.m2/plugin-registry.xml'[DEBUG]Building Maven global-level plugin registry from: '/mnt/local/files/apache-maven-2.2.1/conf/plugin-registry.xml'
Scanning for projects...

Typicall maven stuff.

Now, the second time:

1
2
3
4
5
6
7
Adding reference: ant.PropertyHelper
Detected Java version: 1.6 in: /mnt/local/files/jdk1.6.0_17/jre
Detected OS: Linux
Adding reference: ant.ComponentHelper
Trying to override old definition of task java
 +Datatype java org.apache.tools.ant.module.bridge.impl.ForkedJavaOverride
parsing buildfile jar:file:/mnt/local/files/netbeans-6.8/java3/ant/nblib/org-netbeans-modules-ant-browsetask.jar!/org/netbeans/modules/ant/browsetask/antlib.xml with URI = jar:file:/mnt/local/files/netbeans-6.8/java3/ant/nblib/org-netbeans-modules-ant-browsetask.jar!/org/netbeans/modules/ant/browsetask/antlib.xml
It’s an ant execution !

I don’t know why, but the second time Netbeans is running the unit test with Ant, and no more Maven. How can it be possible ?

What’s the point ? I wouldn’t care less if that didn’t make my test fail….

Writing the Ultimate Form – Part 2

9 mars 2010

Basic error handling

Seam, JSF and Java all team up together to help developers in displaying error messages to the user.

JSF provides the basis, with the FacesMessage mechanism. Anywhere in your code, you can create FacesMessages, and add them to a FacesMessages object. Then, when the next page displays, the error messages are displayed to the user.

You can use special tags for that in your .xhtml page:

1
2
<ice:messages id="messages" globalOnly="true" styleClass="message"
    errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"/>

Notice icefaces allows us to set various css style depending on the type of error.

The result is here:

Now, when a validation is bad, we want to highlight the input field, then display the error message next to it. Fortunately, JSF allows a developer to assign an error message to a field, and by default the validators and converters do just that.

And Seam, allows us to define specific html code when a field is in error. This is done using the s:decorate tag, and the best option is to refer to a template file that will be reused for all fields.

Now the input field is declared like that:

1
2
3
4
5
<s:decorate template="/layout/decorate.xhtml">
  <ice:inputText id="nbElements" value="#{searchPage.nbElements}" />
  <ice:message for="nbElements" styleClass="message"
    errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"/&gt;
</s:decorate>

and for the field that must not be null or empty, we just add an required= »true » attribute:

1
2
3
4
5
<s:decorate template="/layout/decorate.xhtml">
  <ice:inputText id="notNull" value="#{searchPage.notNull}" required="true" />
  <ice:message for="notNull" styleClass="message"
      errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"/>
</s:decorate>

and the content of the error template file is this:

1
2
3
4
5
6
7
<div class="errorDiv">
  <span class="#{invalid?'errors':''}>
    <s:validateAll>
      <ui:insert/>
    </s:validateAll>
  </span>
</div>

The errors css style just sets the component border to Red.

Now, if we try some basic errrors, like by inputing letters in a field mapped to an integer value, here is the result:

Notice the error message is more precise than the previous screenshot. Any error message can be changed in JSF by a simple properties file.

That’s all for basic error handling. Next time, we’ll try to see the behavior of the current page for all errors defined in the first part.

Writing the ultimate form

26 février 2010

Using Seam, JSF and IceFaces, I’m trying to write THE form.

I mean, even the basic stuff like a good form is not easy to do, and we’re experiencing some troubles in our project.

So I decided to try to write the « perfect » and « working in all cases » form.

What features are needed ?

  1. Input must be validated. A integer field should not accept letters for example.
  2. Fields can be mandatory or not
  3. Erroneous fields should be highlighted, with an error message.
  4. I want to select easily a date. Moreover, I want to select it, even if a mandatory field has not been set.
  5. A Field can update values and rendering of other fields. Even if some fields contains errors

Well, that’s all for now. I can’t think of others features, but I’m quite sure I forgot some. We’ll add it later.

The basic:

For beginning, I’m just going to write an input field accepting integers, a date selector, and another field that is mandatory.

To do this, a few xhtml lines of code are needed:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
<ice:form id="searchForm" >
  <ice:panelGrid id="homePanelGrid" columns="2" columnClasses="rightMenu,leftMenu">
    <ice:outputLabel value="Nb Elements:" />
    <ice:inputText id="nbElements" value="#{searchPage.nbElements}" />
    <ice:outputLabel value="Not null:" />
    <ice:inputText id="notNull" value="#{searchPage.notNull}" />
    <ice:outputLabel value="Start Date:" />
    <ice:selectInputDate id="startDate" value="#{searchPage.startDate}" renderAsPopup="true" />
  </ice:panelGrid>
  <ice:commandButton id="searchAction" value="Search" action="#{searchPage.search}" />
</ice:form>

Quite easy, and the result is here:

Now, if I try to enter an invalid value in the integer field and press the button:

No error message is displayed…

We’ll see in our next chapter how to handle error messages.

Datanucleus maven plugin hell

23 février 2010

As Google App. Engine uses a datanucleus plugin for database access, I’ve now switched to it (and leaved Hibernate).

Datanucleus uses bytecode instrumentation for persistance mapping, but not in real time like Hibernate does. Your classes must be enhanced just after compilation.

With maven, it’s not a problem for me, as described here:
http://www.datanucleus.org

Netbeans + Linux + Maven + datanucleus-plugin = hell

Using datanucleus-maven-plugin 1.1.4 (don’t use version 1.1.3 it will force download of Datanucleus 2.0), I get an error when running the compilation under Netbeans.

1.
2.
Embedded error: Error while executing process.
java.io.IOException: error=2, No such file or directory.

I then enable the log for the plugin to see what’s happening, but get no logs. No logs at all…

Of course, nothing in the doc about this problem, so I finally searched the plugin source code:

I see that the plugin is calling « java » directly to process the enhancements. And it seems that when Netbeans calls Maven for compilation, it doesn’t provide the linux path, so the plugin couldn’t start java.

I noticed then a hidden or not-so-documented option, fork, that if set to false, will call directly the bytecode enhancer with no calls to « java ».
Here is the maven configuration to use:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
<plugin>
  <groupId>org.datanucleus</groupId>
  <artifactId>maven-datanucleus-plugin</artifactId>
  <version>1.1.4</version>
  <configuration>
    <fork>false</fork>
    <mappingIncludes>**/*.class</mappingIncludes>
    <api>JPA</api>
    <log4jConfiguration>
      ${basedir}/src/test/resources/log4j.properties
    </log4jConfiguration>
    <verbose>true</verbose>
    <enhancerName>ASM</enhancerName>
    <props>
       ${project.build.testOutputDirectory}/datanucleus.properties
    </props>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.apache.derby</groupId>
      <artifactId>derbyclient</artifactId>
      <version>10.5.3.0_1</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.12</version>
    </dependency>
  </dependencies>
</plugin>

So I tried, and this worked like a charm.
PS: The other plugin, schema-create doesn’t have this option, so I have to run it from command line.

Fragmented Java or no Java ?

30 juin 2009

I’ve just read the last interview from Gosling - See here.

With his usual straight-to-the-point language, he expressed concern about Google and the free Android. Basically, his point is that, as any operator can modify Android, you will have different versions of it and a hard time making your Android application work on all phones.

He’s right.

But personnally I prefer a fragmented platform that is Java based than a non-fragmented one that doesn’t allow Java applications.

Of course, a complete non-fragmented Java platform is much better, but it seems to be too slow-moving and to stiffle most innovation….

GC.

Share an xsd with multiple webservices using maven, netbeans, jax-ws and jaxb2

27 juin 2009

My work really consist in making different tools and java framework work together. I need to find solutions so that any developper can understand and use them afterwards.

This time the project involves developing lots of web services, and some batch treatments accessing to a single and complete business model. The business model is defined by an XSD file. This file will often change during the lifetime of the projet, so I need some automatic solutions. I decided to have a library containing the Java classes of the Business Model, and this library will be used by the web services and the batches. I’m using Maven, jaxb2 and jaxws, all under Netbeans, and I will explain how I managed to do that.

Generating jaxb2 beans using Maven:

First, I use the maven jaxb2 plugin to generate the corresponding Java Classes. Netbeans doesn’t support Jaxb2 mapping with Maven (only with ant), so I had to find and modify the pom.xml file manually. Once properly configured (with generated classes under target/generated-sources/xjc), the generated source code is automatically recognized by Netbeans (and greyed so that you know it’s generated, like in the screenshot below).

Creating the WebService:

Next, I create the .wsdl files, I’m not really expert in web services, so I create a web service using Netbeans (with the option to generate a wsdl from java classes), this was an easy way to get the .wsdl. Then I discard the other generated files from Netbeans, and told it to generate again the web service, this time using the .wsdl tweaked to import my Business Model .xsd.

The nice thing with Netbeans 6.7 and Maven, is that Netbeans modifies your maven project when you create a web service. The web service generation is really done by the maven project (using jax:ws plugin) and so is not dependent at all of Netbeans. Using other IDEs, you’ll have to create the web service using the wizards, and then manually modify the Maven project. Below is the modification made by Netbeans to the maven project file:

The problem:

When generating the Java source from the wsdl, the imported xsd is mapped again to Java classes. With the same files than the ones generated by Jaxb2. So I tried to tell the jaxws plugin that the .xsd file was already mapped. Documentation says you can use « episode » files, but this only works if the namespace of the webservice is different from the namespace of the XSD. That was not the case.

One solution is be to re-generate the source code, and delete the duplicated source so that only the classes in the business model project are used. That should work, but I need to keep some generated java classes: The ones from the Web Service itself. So I want to generate the .xsd classes in another package than the classes from the .wsdl (all with the same namespace !!)

The solution:

After lots of trials and errors, I eventually managed to do this by carefully crafting two files: one for jaxb and the other for jaxws, like this:

The content of  bindings-jaxb.xml:

And the content of bindings-jaxw.xml:

I get the web service interface and factory generated  in com.xxx.xxx.ws.mediation.service, and the source code from the business model in com.xxx.xxx.sas.data. It was then really easy to tell maven project to delete duplicates generated source code before compilation.

And as Netbeans compiles using maven, I get this behavior completely integrated into my IDE.

GC.

Netbeans compared to IDEA #2. Maven again

8 avril 2009

Following the first post about « using Netbeans for an IDEA user », I would like to add some more informations about maven support.

Skipping unit tests:

Each time I compile my maven project in Netbeans, it runs the unit tests, even for small changes. Fortunately, you can easily tell Netbeans to avoid unit test during compilation. Simply right-click on the project, then select Properties. You can check the « Skip tests » control like in the dialog above:

Importing a maven Web project:

I have successfully opened a Maven project that generates a .war file into Netbeans. It is recognized as a web project, you simply by sets the target application server, and then you can run and debug your web application.

It generates the .war file using Maven, and deploys it automatically to the application server.

The neat feature with Web projects:

It’s easy to use, and great for day-to-day web development: It’s tedious to wait for Maven to generate the war and deploys it each time you modify a .jsp or a .xhtml page. So Netbeans automatically deploy the webapp resources files when you save them. Great ! And IDEA does it too.

Android Game Development #3: Games specific

5 avril 2009

In this third installment of the Android Game Development series, I’ll try to explain how to construct the project. I know the LunaLander sample directly uses thread and SurfaceHolder.Callback, but my first goal is simply to display an image, I will complexify later.

Games don’t use Views

In Android, most of the games don’t really use the View hierarchy of objects, because it’s too slow and cumbersome. They instead write directly to a Canvas.

In order to get the Canvas displayed to the screen, you create a SurfaceView object.

Thus, the page is a  FrameLayout, and inside we put our SurfaceView class, as shown in the main resource file:

How to insert an Image into Android project ?

Using Gimp, I have drawn a small red ball:  Nice isn’t it ?

How can I use it in my project. Well it’s quite simple, you copy the file in your project res/drawable subdirectory, you update your Eclipse Project (F5 key). Automagically, you see your new image in the project, and the Eclipse Plugin regenerates the R class so that you can easily access the resource in your code or Layout file. Just take a look at this sample (called by the constructor of our SurfaceView):

We can now easily override the onDraw method on our SurfaceView derived class, and display the image:

Notice we set the position and dimension to display in the image itself, and tells it to display into the canvas. I would have expected the other way round (calling a Canvas method to draw the image).

I run the  project and here is the result :

Well something got wrong, but what ? Maybe I should check in Android documentation how to see some sort of logs or whatever, but for now, each time I get this, I’m clueless about what went wrong.
This time it was easy, I put the wrong class name in the layout.xml file, and it couldn’t find my SurfaceView class. Too bad Eclipse didn’t warn me about this. I fixed the error, started again, and then:

A Black screen. No Error but no red ball neither….

Running it under debugger shows me that the onDraw method on my SurfaceView class is not called !

After some more experiments, (checked the LunarLander sample, tried to draw a big green square on Canvas, etc….) I finally managed to make it work.
To have the SurfaceView displayed, you have to set a background color. With my init method like that:

I get the « good » result as follow: Yeepeee !!!


GC.

Android Game Development #2: Discovering

31 mars 2009

Welcome into part 2 of the Android blog series !

Today, I will try to explain the concepts I discovered when creating the project.

Finding a Sample:

When trying new libraries or new functionalities, I always try to find sample code I can analyse and copy to my own project. Fortunately, Android project pages contains such samples.

One of them seems to be a perfect starting point for my game: The Lunar Lander Sample. It’s a classic game where you want to land a lunar module by switching off and on reactors. The difficulty is that the module follows gravity rules, and you really need to predict it’s behavior.

Remember this game now ?

The documentation explains to a great extend the source code of the sample, so I won’t repeat it here. Well, just in two words then.

General Guidelines:

The Main class is called an Activity. An Activity is like a controler (in MVC pattern), there can be many activities in a single application. Think of it has a screen in your application. It creates a View class, either programmatically or by reading an XML description file. The View class represents, errr…. the View part of the MVC pattern.

All images, view xml files and string resources are parsed by a sdk tool that generates a class, the R class (what’s this name ???), that contains the Ids of these resources. You can write your application like that:

- You design you application U.I. using the XML file (and the plugin shows you instantly the result).

- You override the  onCreate function in your activity class to load and set the view to display.

- From the View class you just created, you get the Java classes of your controls  (buttons, input fields) by using their Ids in the generated R.class

-  You then can get / set  the values of these controls, listen to a click, etc….

- Of course, for a game, you don’t use the View classes hierarchy, but directly draw into a canvas. More on that later.

NB: The eclipse plugin handles SDK tools for you.  It re-generates automatically the R.class (Did I told you the name was awful ?) whenever you create a new resource, it immediatly shows a bad reference in your layout file, it can display the result of your layout (screen below), and it packs the project and installs it into the emulator when you click on the Eclipse Run button.

GC.

Android Game Development #1: The project

31 mars 2009

As a side project, I’m trying to develop a small game in Android.

As you know, Android is the new open source platform, designed by Google, for smartphones.

It is based on Linux, and the language used to develop applications on it is Java. So I decided to try to develop a small application for it: A game.

I will try to  explain here the things are discovered in this journey.

The project:

A very simple game that uses the touch screen capabilities of the phone: You throw a ball using you fingers and it must either go into holes, avoid pitfalls, bump into, errr…. bumpers and so on. You will have many different levels with many kinds of obstacles to overcome. This game will involve precision in your fingertips !

Starting:

First I downloaded the Android SDK, along with Eclipse and it’s plugin. All is clearly explained here and installs without an itch. Along with the SDK comes a smartphone emulator, that shows this:

Nice no ?

The Eclipse plugin integrates Android SDK seamlessly with eclipse: You can create a new android project, run it in the emulator, design the screen (with preview), etc….

Next part will talk about creating the game project using a provided sample  project.