Friday, May 18, 2018

 

Basic Maven 2 POM for Groovy 2.4

I recently had to switch a Groovy 2 project from an Ant build to a Maven 2 build. This involved a fair amount of searching and trail and error so I thought I'd post the basic pom.xml that got the job done.

I elected to use the groovy-eclipse plugin instead of the alternatives described in the Compiling Groovy documentation. I also chose the "Do Nothing" option of putting the .groovy files under src/main/java because they were already mostly there in the existing project. This went fairly smoothly once I got past some problems with the plugin versions. Note that I included the pluginRepository for the compiler plugin in my pom.xml. I did that for getting this up and running, but I'll eventually move it to settings.xml.

Here's the pom.xml...
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xyz</groupId>
  <artifactId>abc</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>abc</name>
  <url>http://maven.apache.org</url>

  <pluginRepositories>
    <pluginRepository>
      <id>bintray</id>
      <name>Groovy Bintray</name>
      <url>https://dl.bintray.com/groovy/maven</url>
      <snapshots><enabled>false</enabled></snapshots>
    </pluginRepository>
  </pluginRepositories>

  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <classifier>indy</classifier>
      <version>2.4.15</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <compilerId>groovy-eclipse-compiler</compilerId>
          <compilerArguments>
            <indy/>
          </compilerArguments>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-compiler</artifactId>
            <version>2.9.2-04</version>
          </dependency>
          <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-batch</artifactId>
            <version>2.4.15-01</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

Labels: , , ,


Monday, June 23, 2008

 

Basic JavaMail Code

Every time I have to use the JavaMail API I am reminded how much I don't like it. Could it be any harder to figure out how to send a simple email?

Here is an example of how to send an HTML email (minus the exception handling)...

Properties props = new Properties();
props.put("mail.smtp.host", "YOUR_HOST_NAME");

Session session = Session.getInstance(props, null);
session.setDebug(true);

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("YOU@YOURHOST.COM"));

InternetAddress [] address = {new InternetAddress("PERSON@THEIRHOST.COM")};

message.setRecipients(Message.RecipientType.TO, address);
message.setSubject("A Subject");
message.setSentDate(Calendar.getInstance().getTime());
message.setContent("<html><body>This is an HTML email.</body></html>", "text/html");

Transport.send(message);

In the end it looks fairly simple, but it took far too much time searching through the JavaMail docs to piece them together.

Labels: ,


Sunday, January 28, 2007

 

Java SE 6 Presentation

In January 2007, I gave a presentation to the Kansas City Java User Group (KCJava) about the recently released Java SE 6. The presentation gives an overview of the major features that are included in this release of the Java SDK and some links where one can learn more about each of them.

Labels: ,


Friday, August 04, 2006

 

Basic Castor XML Example

Castor XML is an open-source framework that simplifies the task of marshalling Java objects to and from XML documents. I chose Castor over JAXB because after a little research it sounded like Castor was easier to configure and that it would be better at handling XML documents for which I do not have an XML Schema.

In this article I will show how to use Castor to create an XML document from some Java objects. Then I'll demonstrate going in the other direction - reading an XML document and populating some Java objects.

First, create the Java classes. For this example I'll create a simple User class with an Address class. Note that each of the classes implements Serializable and has a default constructor. Each of them also adheres to the JavaBeans naming conventions. This isn't a requirement but if you don't then you must provide a mapping file.

// from User.java
public class User implements java.io.Serializable {
private String firstName;
private String lastName;
private java.util.Date birthDate;
private int numAnnoyingHabits;
private Address address;

public User() {
}

public Address getAddress() {
return address;
}
public void setAddress(Address a) {
address = a;
}
public java.util.Date getBirthDate() {
return birthDate;
}
public void setBirthDate(java.util.Date d) {
birthDate = d;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String name) {
firstName = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String name) {
lastName = name;
}
public int getNumAnnoyingHabits() {
return numAnnoyingHabits;
}
public void setNumAnnoyingHabits(int i) {
numAnnoyingHabits = i;
}
}

// from Address.java
public class Address implements java.io.Serializable {
private String address;
private String city;
private String state;
private boolean isPrimary;
private int numberOfYearsActive;

public Address() { }

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public boolean isPrimary() {
return isPrimary;
}

public void setPrimary(boolean isPrimary) {
this.isPrimary = isPrimary;
}

public int getNumberOfYearsActive() {
return numberOfYearsActive;
}

public void setNumberOfYearsActive(int numberOfYearsActive) {
this.numberOfYearsActive = numberOfYearsActive;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}

Then create a class that populates the objects and calls Castor to dump them to XML.

// from Main.java
import java.io.*;
import org.exolab.castor.xml.*;

public class Main {
public static void main(String [] args) {

// first populate objects
User user = new User();
user.setFirstName("Afirst");
user.setLastName("Alast");
user.setBirthDate(new java.util.Date());
user.setNumAnnoyingHabits(2);

Address addr = new Address();
addr.setAddress("100 A Blvd");
addr.setCity("Peculiar");
addr.setState("MO");
addr.setPrimary(true);
addr.setNumberOfYearsActive(4);

user.setAddress(addr);

// now call Castor to write them to XML
try {
Writer writer = new FileWriter("user.xml");

Marshaller.marshal(user, writer);

writer.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}

Next, find all of the required libraries below, place them in the CLASSPATH, and compile the classes. The exact versions that I used are not required.

castor-1.0.1.jar
log4j-1.2.13.jar
commons-logging-1.1.jar
xercesImpl.jar (Xerces 2.8)

Finally, create a castor.properties file containing the following line and place it in the CLASSPATH. This setting causes Castor to indent the XML file so it is easier to read. In a production environment you probably want to turn indentation off so you're not moving around unnecessary whitespace.

org.exolab.castor.indent=true

When you run the example you should get a document that looks like the following:

<?xml version="1.0" encoding="UTF-8"?>
<user num-annoying-habits="2">
<birth-date>2006-08-05T21:42:46.355-05:00</birth-date>
<address number-of-years-active="4" primary="true">
<address>100 A Blvd</address>
<state>MO</state>
<city>Peculiar</city>
</address>
<last-name>Alast</last-name>
<first-name>Afirst</first-name>
</user>

There are some interesting things to note about the XML:

Now with just a few more lines of code I can read the document back into Java
objects.

// add to Main.java after marshalling code

// unmarshall file to a new object
Reader reader = new FileReader("user.xml");

User user2 = (User) Unmarshaller.unmarshal(User.class, reader);

That's all there is to it! In future blogs I'll show how Castor handles collections and then how its mapping file can be used to configure Castor to read and write XML documents that are not in its default format.

Labels: , ,


Sunday, July 23, 2006

 

Object to XML Binding

Recently I've gotten to work on some projects that require object to XML binding. The best part is that one is in .NET while the other is in Java. This has given me the opportunity to compare and contrast how each platform accomplishes this task. In the next few weeks I'll try to post my findings and some examples of each of them.

Saturday, May 20, 2006

 

Intro to Web Services Presentation

In May 2006, I gave another presentation to the Kansas City Java User Group (KCJava). The topic of this presentation was an introduction to Web Services.

The presentation gives a high-level picture of what Web Services are and what are its key standards and technologies. It also shows the Java API's that support Web Services. Finally there is an example service and client built using Apache Axis.

This was my first in-depth look at Web Services and I was surprised at the complexity of the subject. In contrast, I was surprised at how easy Apache Axis made it to implement a Web Service. I also found that the Java Web Services Developer Pack (JWSDP) 2.0 has some similar features that I plan to learn more about.

Labels: ,


Wednesday, May 17, 2006

 

Ajax Presentation

About a month ago with the help of Shashank Date I gave an introductory presentation on Ajax to the Kansas City Java User Group.

During my research I found Ajax to be much more fascinating than I had expected. My expectations were that it was just a technology for "spicing up" web interfaces with simple features like auto-completion text fields.

However, articles like Jesse James Garrett's made me realize that Ajax has a much grander vision. The vision is what is represented by his "Ajax engine". When done correctly, this layer will decouple the web user interface from the web itself. Instead of being constrained by the request-response model of the web, the web interface will interact with the services provided by the Ajax engine. In doing so the web interface can be much more responsive and feature-rich.

All of this is a pretty tall task given that Ajax relies on the major browsers to uniformly implement Javascript. However, it would be nice to be able to use web applications that are as interactive as today's desktop apps.

This page is powered by Blogger. Isn't yours?