Icon

Software Engineering, Architecture, Web Development and beyond…

Creating new projects with Leiningen

Leiningen is the “maven” for clojure. It is a build management tool which you can basically use to compile, package your Clojure projects and manage dependencies. In this introductionary tutorial, i will show you the basics to create clojure projects on an unix environmnent using Leiningen.


Installing Leiningen:

$ pwd
/home/bagdemir
$ mkdir leiningen
$ export PATH=$PATH:/home/bagdemir/leiningen
$ cd /home/bagdemir/leiningen
$ wget http://github.com/technomancy/leiningen/raw/stable/bin/lein
$ chmod +x lein
$ lein self-install

Now we can test lein calling

$ lein version
Leiningen 1.6.0 on Java 1.6.0_24 Java HotSpot(TM) Client VM

We can now create a new project using new:

$ lein new localwebapp

After calling new, lein creates a structured project folder, project file and a README file for you. If you look into the project file, project.clj, you notice that the project file is also a clojure file not a xml as compared with maven’s pom.xml:

(defproject localwebapp "1.0.0-SNAPSHOT"
  :description "FIXME: write description"
  :dependencies [[org.clojure/clojure "1.2.1"]])

defproject macro is followed by a symbol which defines project name and version. The reminder elements are in key-value format like “description” which indicates project description and the dependencies are project dependencies.
Dependency management in lein is like maven’s. You can define your dependencies after :dependencies key in a vector, [ [GROUPID/ARTIFACTID "version"] … ]. Calling

$ lein deps

causes all the dependent libraries to be downloaded from repositories and copied, first, into local repository (on my machine /home/.m2/repository) and then after into the “lib” folder in your project which will be also created by lein.
You can also add some repositories in project.clj like this:

:repositories {"apache-releases" "http://repository.apache.org/content/repositories/releases/"}

Some useful commands are:

$ lein clean
$ lein compile 
$ lein jar
$ lein uberjar

which deletes all your files created during build including lib folder, performs compilation of clojures into java classes, and packages project as jar and later packages also, but with all dependencies including libraries to create an independent jar – if this jar is an executable one, then you have to introduce a namespace as an entry point defined with :main in project.clj.

Bean Validation (JSR-303) and validating Cron Expressions, extending constraints

To validate cron expressions using bean validation stuff, the best practice is to write your own custom validators and to use quartz framework’s cron expression facilities.
To write your custom validator, you need to implement

ConstraintValidator<A extends java.lang.annotation.Annotation,T>

from javax.validation.* package.

public class CronExpressionValidator implements
        ConstraintValidator<CronExpression, String> {
 
    public void initialize(CronExpression cronExpression) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
 
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        return org.quartz.CronExpression.isValidExpression(s);
    }
}

To validate cron expressions in isValid() method, the isValidExpression() static method is used from Quartz Framework. To add Quartz to our maven project add the following dependency into your pom:

        <dependency>
            <groupId>org.opensymphony.quartz</groupId>
            <artifactId>quartz</artifactId>
            <version>1.6.5</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

CronExpression defined as parameterized type is the annotation class that we will use for cron constraints and seems like following;

@Documented
@Constraint(validatedBy = CronExpressionValidator.class)
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface CronExpression {
    String message() default "Cron expression is not valid!";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

@Documented indicates that annotations with a type are to be documented by javadoc and similar tools by default.
@Constraint(validatedBy=CronExpressionValidator.class) defines validator class.
@Target defines that this annotation is applicable to methods, fields, annotation types, constructors and parameters.
@Retention defines that the annotation is a runtime annotation not a compile time annotation like @SupressWarnings, @Override etc.

We are now ready to annotate our fields, parameters etc. with our new constraint just like this:

class MyCronBean {
    @NotNull
    @CronExpression
    private String cronExpression;
    /** setters and getters */
}

Netbeans: Adding a project license to your project

I see lots of questions in Netbeans forums about Templates and project license and developers has problems with configuring it. If you want to add a project license and associate it with your project, you need to add a license property in your project configuration file. The property file “nb-configuration.xml” lies in your project folder, in my case:

Erhan-Bagdemirs-MacBook-Pro:NetBeansProjects merodach$ pwd
HOME/NetBeansProjects

You have to add just”” in your properties:

<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
     ...
    <netbeans.hint.license>mylicense</netbeans.hint.license>
     ...
</properties>

and create a new licence file under:
HOME/.netbeans/6.9/config/Templates/Licenses

after a new installation of Netbeans (i have the version 6.9.1), there was no License folder. If you can’t see this folder, just create it and create a license file accordingly:
HOME/.netbeans/6.9/config/Templates/Licenses/license-mylicense.txt

The name of the license file is important and must follow this naming template:
license-${project.license}.txt

${project.license} is the property which you added with ““.

Open “Template Manager” on your IDE, following the menu path “Tools > Templates”. For Java classes the template seems like this if you open template in your editor:
null
and you can see the path to your license and how it is associated with your project. We make no changes in the template and just create now a new java class to test our license (in my case, apache’s open source license):

null

erhan

Author


Hello, I'm Erhan Bagdemir and this is my blog. I talk about Java, J2EE, Frameworks, web application development, OOAD and various other topics often related to programming.

Erhan Bagdemir  Profil von Erhan Bagdemir auf LinkedIn anzeigen

ebagdemir on Stackoverflow

ebagdemir on Twitter

    Hamburg

    Slideshow