Friday, 24 January 2014

There will always come a point in a Maven based project where you have to execute different parametrized Maven builds and their number increases and increases...

IDE support for Maven builds

Fortunately you favorite IDEs support saving of different Maven builds: Eclipse with its launchers, NetBeans with the customized Maven builds submenu.
But their storing of build informations is different: Eclipse creates *.launch files and NetBeans nb-actions*.xml files. If they are checked in colleagues will be "not amused" to see these config files, when they happen to use another IDE (I know that officially a project always uses the same IDE... but you know the truth!). So is there a unique way that serves all IDEs/developers well?
Yes, the good all well supported Ant world!

Execute Maven Builds from one Ant build.xml

Here is an Ant build.xml that executes the standard Maven goals:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Maven Launcher">
  <property name="maven.executable" value="mvn.bat" />
 
  <target name="mvn package">
    <exec executable="${maven.executable}">
      <arg value="package" />
    </exec>
  </target>

  <target name="mvn test">
    <exec executable="${maven.executable}">
      <arg value="test" />
    </exec>
  </target>

  <target name="mvn clean package">
    <exec executable="${maven.executable}">
      <arg value="clean" />
      <arg value="test" />
    </exec>
  </target>
</project>
This gives you a first impression of how to call the "mvn" executable and pass command line arguments to it. Following this it is easy to call even more parametrized (with system properties and profile) builds:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Maven Launcher">
  ...
  <property name="maven.executable" value="mvn.bat" />
 
  <target name="Performance Tests (Backend, offline)">
    <exec executable="${maven.executable}" failonerror="true">
      <arg value="clean" />
      <arg value="verify" />
      <arg value="-Pperformance" />
      <arg value="-Dscope=backend" />
      <arg value="-Dtest.env=DEV" />
      <arg value="-o" />
    </exec>
  </target>
  ...
</project>
That's it! Now you can put all your Maven calls into one place. Give them reasonable names and it is easy to navigate through the build.xml using your IDE's Ant view.

Thursday, 16 January 2014

After our short introduction about what Swing / JFC is, it is time to code our first Swing GUI app. It is a simple HelloWorld app being nothing more than a window containing a "Hello World" text. But what sounds simple, is already the basic for all GUI apps you will code in the future.
The Java Tutorial introduces the "Hello World" app in the chapter "Compiling and Running Swing Programs" and explains the used Swing component "JFrame" in "How to Make Frames (Main Windows)".

The "Hello World" app's code

File "HelloWorldSwing.java":
public class HelloWorldSwing {
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add the ubiquitous "Hello World" label.
        JLabel label = new JLabel("Hello World");
        frame.getContentPane().add(label);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Compile the Java app's code

From within the directory where the Java-file is, issue the following command to compile the app:
javac HelloWorldSwing.java
This results in a bytecode file "HelloWorldSwing.class".

Run the Java app

Execute your app with
java HelloWorldSwing
It may start in the screen's corner and too small sized, but you can enlarge the window by dragging the right lower corner and move the window with drag and drop.
Here it is (resized to see all decorations, the title and the content):
Depending on your platform the style of the window decorations may vary.

That leads us to the first question: How to center the window on the screen?".

The following posts are all about programming GUI applications with the mature Java GUI Framework called "Swing".
We will learn and build up a full blown cross platform File Manager step by step following documentation of "The Swing Tutorial" and lurking into code of other open source Java applications.

What is Swing?

The tutorial section "About the JFC and Swing" explains:
JFC is short for Java Foundation Classes, which encompass a group of features for building graphical user interfaces (GUIs) and adding rich graphics functionality and interactivity to Java applications.

Where to start from?

So it is a framework for building graphical applications you can use with a mouse and keyboard or a touchscreen. Nowadays a GUI application is usually started as a "window" (a rectangle box containing the apps' components).
And that is the first thing we want to create: a window.
Follow now our next posts about creating step by step a full blown GUI app.