Friday, 1 November 2013

Cool tool of the day: Sitemesh

Why Use SiteMesh?

SiteMesh is a lightweight and flexible Java web application framework that applies the Gang of Four decorator pattern to allow a clean separation of content from presentation.

I am posting it here as I have used this tool before and really like it but I just spent 10 minutes attempting to recall what it was called.

The jars for site mesh can be found on the Maven repository or but pasting this into your POM.xml:


<dependency>
    <groupId>opensymphony</groupId>
    <artifactId>sitemesh</artifactId>
    <version>2.4.2</version>
</dependency>
            

Setting up Eclipse (again)

I have documented the setting up of eclipse before but in the almost two years since that post I have learned a few new tricks. SO I'm documenting them here in case they are useful to someone else.

Java JDK

You will probably have Java installed, but it will only be the JRE, which will hot have all the environment variables established. The JDK can be found by searching on Google and it will probably be the top link.
I have this time around down loaded the version 7 JDK from here (http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html). (Don't forget to pick the right version and accept the licensing  terms).

On a windows PC the easiest location to install all of these files is under a single directory structure ... with NO spaces in the name.
I will use "c:\development" throughout this post (cos it's what I use).

So:
  1. Download the JDK installer;
  2. Place it in a new folder  "c:\development\java";
  3. Run the installer (as administrator);
  4. Wait for it to start & press the next buttons;
  5. At the Optional Features deselect the "Public JRE" as you already have one;
  6. Change the installed directory to  "c:\development\java\jdk1.7.0_45\" (where 1.7.0_45 is the JDK version);
  7. Now install by pressing next;
  8. Set an environment variable JAVA_HOME = C:\development\java\jdk1.7.0_45;
  9. Add to the PATH environment variable %JAVA_HOME%\bin;
  10. Open a command prompt and check that the maven command "java-version" runs and gives the correct version.

Maven

Since my last version of this install guide I have been working with Maven, which is a build tool. I had produced a nice set of ANT scripts to do what MAVEN does but I have decided that this is just too much like reinventing the wheel.

So:
  1. Download the zip;
  2. Place into a new folder "c:\development\maven";
  3. Extract the files to  "C:\development\maven\apache-maven-3.1.1";
  4. Create an environment variable M2 = C:\development\maven\apache-maven-3.1.1;
  5. Add to the PATH environment variable %M2%\bin;
  6. Open a command prompt and check that the maven command "mvn -version" runs and gives the correct version.

Eclipse 

With the JDK set up now install the version of Eclipse you prefer.
My current favorite is the Spring Tool Suite (STS) since it comes with may of the tools and plugins pre-installed.

So:
  1. Download the latest STS; from http://spring.io/tools/sts/all ensuring you get the right version for your OS
  2. Place it in a new folder called "c:\development\eclipse";
  3. Run the installer (as administrator);
  4. Accept the licensing agreement;
  5. Accept the default installation location which should be C:\development\eclipse\springsource;
  6. Install all the toys;
  7. Set up the JDK path to be C:\development\java\jdk1.7.0_45;
  8. It may complain that a 32-bit or 64-bit version is needed which means you made a mistake at step #1;
  9. When prompted for a short cut group call it "Java Development";
  10. Launch the tool set to ensure it works;
  11. When prompted for a workspace create one with out spaces ie. "C:\development\projects\default-eclipse";
  12. You may be prompted to install any updates if they exist;
  13. Create a "Hello World" project and check whether you have Java set up correctly;

Eclipse plugins (optional)

Having installed Eclipse it is now a mater of installing a set of plugins. These vary accourding to taste and your development purpose; mine is Java web development.
I've written about the set of plugins I use before (here, & here and here). 
I'm not going to detail which ones I'm just going to list those that I use and include a 'bookmark.xml' import to list them.
The Plugins I use are:
  1. Sysdeo JSP/Tomcat Manager This plugin allows you to start or stop the tomcat environment and perform remote debuging;
  2. Log4e - Log4E is an Eclipse Plugin which helps you to use your logger easily in Java Projects; 
  3. StartExplorer- This plug-in gives you a convenient way to examine a file or a folder outside the Eclipse workspace with your file manager or open a shell/cmd.exe  and do all this by adding a few entries to Eclipse's context menus.
  4. SubEclipse - Links to your subversion source control.
  5. Aptana Studio -  HTML, CSS etc editing
 
 Interesting ones:
  1. C# development

XAMPP

This has to be the sweetest install of the set. It is an entire Apache LAMP stack installer with Tomcat included.
The best version if you are installing to windows is the full instalation tool sooner than the zip version.

So:
  1. Download the latest XAMPP;
  2. Place it in a new folder called "c:\development\xampp";
  3. Run the installer (as administrator);
  4. Follow the next buttons and select all the toys;
  5. Select the installation to c:\development\xampp\1.8.3 (where 1.8.3) is the current version;
  6. Wait a long time for the installation top complete;
  7. Start the control panel and start up HTTPD;
  8. Open a browser and check http://localhost, if you don't get a page titled XXXX then you may have something else running on port 80.
    The usual culprits are IIS or Skype these systems need to be configured to use any thing other than 80,443, 8080, 8443 (which are all ports used by Httpd & Tomcat);
    Shut those services down or change their ports (see articles on IIS or Skype respectivly) and retry (PS. don't forget to force your browser to refresh the page or you could be thinking it is still there).
  9. Tomcat should have its admin user set up.
    You can check this by browsing to http://127.0.0.1:8080/manager/status.
    If it fails shut down tomcat & edit the web-users.xml file (in the above config this is C:\development\xampp\1.8.3\tomcat\conf\web-users.xml);
    Add a set of nodes as follows:
    <role rolename="manager-gui"/>
    <user username="tomcat" password="s3cret" roles="manager-gui"/>
    Then restart.
<< Previously in this series
 

Monday, 21 October 2013

Something done in my down time: "Allrangecombat.co.uk"

Allrangecombat Website
I have been a long term student of martial arts and when ask by my senior instructor whether there anything I could do to assist with his website, of course I said yes.

The site is available at allrangecombat.co.uk and allrangecombat.com and has been hosted on a relatively cheep host iPage.
The whole site took about two days to put together and much of that was messing about unnecessarily with uploading content.
The site is based on a nice little CMS called MODX which is 100% PHP which is installable as a standard extra on iPage. While the CSS is based on MetroUI which attempts to emulate Windows Eight's look and feel. I have used it here to provide a CSS which adapts to mobile devices.

Naturally I have attached my own club site to this one as a mini-site: allrangecombat.co.uk/bath.
Enjoy!
Better still come training!

A blatant appeal for work!

Thursday, 5 September 2013

Cool tip of the day: Maven & Eclipse ... importing projects

Today I attempted to import a project that had been written using MAVEN as its build process.
So I confidently when to the eclipse option to import existing maven projects ... all went well until I opened the project & tried to build it. when I saw this error:

Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-resources-plugin:2.4.3:resources (execution: default-resources, phase: process-resources)   pom.xml /test line 6    Maven Project Build Lifecycle Mapping Problem
This was not very welcome and the eclipse autofix options did not fix the issue.
So I turned to Slastdot & found a post (http://stackoverflow.com/questions/7638562/import-maven-project-to-eclipse-and-fix-the-errors) but the top solution to to do a project update did not work.

So to fix it I followed a secondary solution (after deleting the .project file previously created) ...


  1. Open the project folder in a command prompt;
  2. run command mvn eclipse:eclipse
  3. Open the project using eclipse;
  4. Set project properties "configure-->Convert to Maven Project"
My experience is that this works nicely.... ALMOST!

The issue is that eclipse may not correctly pick up the maven dependencies.
This is because the .classpath file does not include values on the paths as  follows "<attribute name="maven.pomderived" value="true"/>".

This for a simple project means that you needs to convert your .classpath file from this:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
  <classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
  <classpathentry kind="src" path="src/main/java" including="**/*.java"/>
  <classpathentry kind="output" path="target/classes"/>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
</classpath>

to this:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
  <classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
  <classpathentry kind="src" path="src/main/java" including="**/*.java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
  <classpathentry kind="output" path="target/classes"/>
      <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
      <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
</classpath>

Now this should pick up your MAVEN dependencies.

Other references:

  1. http://stackoverflow.com/questions/2037188/how-to-configure-eclipse-build-path-to-use-maven-dependencies
  2. https://groups.google.com/forum/#!topic/scala-ide-user/M9pMpZGD624

Cool tip of the day: Formatting source code for your blog

Oh finally!
I have pasted code into my blog & it has been a real pain to get it right.
While looking around I found a resource on blogspot http://formatmysourcecode.blogspot.co.uk/ which provides an online utility to format the script into html to include on your blog.
It has two outputs, one with inline CSS and the other with a set of styles used.
The "linked style sheet" option depends on a PRE.source-code style.

An example of this formatting is as follows:

/**
 * The HelloWorldApp class implements an application that
 * simply prints "Hello World!" to standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

A second resource can be found at http://codeformatter.blogspot.co.uk/ which has similar options  but allows you to define whether you have line numbers on your code. When you don't use embedded styles it creates a style element which defines two classes pre.CICodeFormatter and code.CICodeFormatter.

An example of this output is as follow:

1:  /**  
2:   * The HelloWorldApp class implements an application that  
3:   * simply prints "Hello World!" to standard output.  
4:   */  
5:  class HelloWorldApp {  
6:    public static void main(String[] args) {  
7:      System.out.println("Hello World!"); // Display the string.  
8:    }  
9:  }  

A third which I have yet to check is "How to use PrettyPrint to format source code in Blogger" which is a utility on blogger.com which is where this site is.
But more on this an other day.

Saturday, 31 August 2013

Transitioning from ANT to MAVEN

Well I have been putting together ANT scripts for some time now and I have created some funky scripts.
My main issue with ANT was that I had to do all the hard work over and over again. I did work to include IVY into my projects but it was just hard work and the scripts grew in complexity.
I had heard good things about MAVEN so I thought I might as well get stuck into it.

After some successes I smacked straight into an issue regarding the precompilation of JSPs which seems to be a dead topic as the plugins do not seem to work correctly or only work if you are building a WAR.
I want to precompile some JSPs and put them into a JAR ... but no joy.
So right now I am looking back at the ANT scripts and looking how best to include them in to MAVEN.

The first clue was a post on Slashdot titled "Multiple antrun tasks in maven".

The key is to have a series of ANTRuns stuck into your MAVEN script as follows:
<plugin>

    <artifactId>maven-antrun-plugin</artifactId>
    <executions>

        <execution>
            <id>clean</id>
            <phase>clean</phase>
            <configuration>
                <tasks>
                    <echo message = ">>>>>>>>>>>>>>>>>>>>>>>>>>clean"/>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>

        <execution>
            <id>compile</id>
            <phase>compile</phase>
            <configuration>
                <tasks>
                    <echo message = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>compile"/>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>

        <execution>
            <id>package</id>
            <phase>package</phase>
            <configuration>
                <tasks>
                    <echo message = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>package"/>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>

    </executions>

</plugin>
asas