The Lazy Dev

Do less. Develop more.  
Filed under

maven

 

OSGi: manually download bundles?!?

I just reached this statement in the Maven Handbook, chapter "Cooking with Maven and OSGi"

"After installing the Apache Felix Web Management console, you will need to install some of its prerequisites."

No one move! What what what? I am missing something. Please tell me I am missing something.
I do not want to believe that after years to make people move to Maven and embrace automatical dependency resolution and download, now with OSGi, the new frontier, I have to manually install jars into a bundle local directory launching something as ugly as:

<pre class="screen"><span><strong class="command">mvn pax:import-bundle \
 -DgroupId=org.apache.felix \
 -DartifactId=javax.servlet
 -Dversion=1.0.0</strong></span>

Come on, tell me this is not true. A bad dream. Please.
--
Daniele Dellafiore
http://danieledellafiore.net

Filed under  //   maven   osgi  

Comments [0]

Quintessential Nexus installation and maven repository configuration

Fortunately there is the Maven handbook [1]. Unfortunately you had to
know that it exists, or you risk to deal with the complete but huge
Nexus documentation. What you need to run a Nexus server is what
follows, giving that you already have JRE and a servlet container
installed (I use Jetty here)

 
cd $JETTY_HOME/webapps 
wget http://nexus.sonatype.org/downloads/nexus-webapp-1.4.0.war 
cd .. 
./bin/jetty.sh restart 

Point browser to http://localhost:8080/nexus-webapp-1.4.0/
Login as admin/admin123

Click on the link "repositories" on the left and choose the "Release"
repository. There is a local path configured as attribute "Default
Local Storage Location".

If you already have a local repositories (in ~/.m2/repository), you
should copy all artifacts to that folder. Like:

 
cp ~/.m2/repository/* ~/sonatype-work/nexus/storage/releases/ 

Then the artifacts will become available to the URL of the Release
repository. Now adding that repository in your pom.xml will make Maven
use those artifacts during build.

That's all.

[1] http://www.sonatype.com/books/mhandbook/reference/

Filed under  //   maven   nexus  

Comments [0]

Multi brand (skinned) application with Maven 2 and Wicket (take 1)

In this two parts article, I will explain how to configure a web application built with Maven to support "branding", or skinning. That is, support different distribution skinned with particular images, logo, background, text. In the end we just want to keep separated the static contents and choose the right set when we package our application so that the result is a war with just the content for a single company. I call this a branded distribution using brand as a similar word for skinning. Brand looks more enterprise and general than skinning, anyway :)

In the second part of the article I will also cover the Wicket side of the story. The Maven side works by himself, anyway.

Some thoughts, before to start.

Fundamentals stuff, but important to remember: files that are in the war root are accessible from an HTML file with a relative path, so if we can have an image under myapp.war/images/pic.png we can acess it with src="images/pic.png".
Instead, when we need to load an image or a text file from Java code, it must be in the Classpath, so it had to be under myapp.war/WEB-INF/
Fundamentals, I told.

Another thing: we want to develop in comfort under our IDE, spcifically Eclipse, not being doomed to perform a Maven compile every time to filter some property. Test should run with their own resources, build path in Eclipse should be consistent and not be affected by multi brand configuration so that Eclipse copy modified resources on the fly and we have latest version in place without going to ask Maven again.

Last thing: as always, use what already exists. Wicket has a built-in support for skinning (called styling) and Maven provides "profiles". Let's use everything we can. Ready, go!

Maven Side - Profiles

Let's first introduce the concept that we are running our application in a particular mode, a brand mode, whatever. Comes natural to define a maven property in pom.xml

 <properties>
             .....
            <brand>stage</brand>
</properties>

Where "brand" is the name of the property and "stage" is the value. For those not familiar with Maven properties, let's say just this: in every file that will be processed by maven during a build session, maven will "filter" all the variables and they will be substituted by the value defined in pom. In a few words:

${brand}  --> stage

in every file filtered by Maven. Included pom.xml itself.

So we will have different brands: stage is my word for "demonstration application". We can have companyA, companyB, and so on. To implement different brands, in maven, we use maven profiles. While stage is a top level POM property, we now define:

   <profiles>
      <profile>
         <id>companyA</id>
         <properties>
            <brand>companyA</brand>
         </properties>
       </profile>
   </profiles>

As we all know, to activate a profile we add "-P companyA" in the maven command line and all the profile configuration will override the default ones. So now we have a default brand, stage, and a companyA specific profile.

Maven Side - Resources

In my stage brand I have to use free images, a personal disclaimer and avoid all contents owned by my customer. So I create a folder "brands"  with a sub folder for each "brand" [1]. So in "src/main/brands/stage" I put my free to use contents.

To let maven choose the folder accordingly to the chosen brand, we configure the new resource this way:

<resources>
         <resource>
            <filtering>false</filtering>
             <directory>src/main/brands/${brand}/</directory>
            <includes>
               <include>**/*.txt</include>
            </includes>
         </resource>
     ...
</resources>

In my particular situation, I am interested in having just *.txt files under my Classpath. Please note that I have also put every file in the brand folder under a sub folder I call "contents", so I do not  have a mess of files in the root of my war. Here is how I load resources from java:

getClass().getClassLoader().getResourceAsStream("contents/" + "myResource.txt")

Now let's deal with static images, typically some logo, background and so on. Target is to access images from HTML file simply with an img tag and a src="contents/header.jpg"

We need to configure what in maven are called "web resources", and deal with the maven-war-plugin:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                  <resource>
                     <directory>src/main/brands/${brand}/</directory>
                     <includes>
                        <include>**/*.jpg</include>
                         <include>**/*.gif</include>
                        <include>**/*.png</include>
                     </includes>
                  </resource>
               </webResources>
             </configuration>
         </plugin>

In the end, the "src/main/brands/${brand}" folder is just a new folder threated as maven normally threat "src/main/webapp" where we normally found the css folder and other folders with static content. This is the reason I normally keep files under "src/main/brands/${brand}/contents", to keep war root clean.

To summer up, here is the folder layout:



src/main/


  - brands


    - stage


      - contents/disclaimer.txt


    - companyA


        - contents/disclaimer.txt


  - webapp


    -css



This completes the maven side of the problem. In the next article we will attack the "web" side, with Wicket, and finish with fixing the development environment.

[1] Thanks to Simone Bordet for the final tip on this on the JUG Milano mailing list! :) http://bordet.blogspot.comt.

Filed under  //   brand   maven   skin   wicket  

Comments [2]

How to create maven archetype and a Catalog for project automatic generation

As many that are using maven 2 since years, I have many mavenized projects and the most of them have similar structure: are web projects, uses jetty and other plugins, many shares a lot of dependencies. So I decided to create an archetype to quickly generate new projects. I also wanted to use the mvn archetype generate command, so I prepared a archetypes catalog in my repository to allow me to use my personal archetypes with automatic generation.


As a start, I took an existing mvn archetype source code. Yes, maybe there are a good tutorial but what is better than an existing simple projects to modify? I am the lazy dev after all. I choose wicketrad-archetype, for the only reason I already had the code on my computer.

There are just two things to do:

  1. Change group and artifact id in the archetype pom (the one in the root folder) with what you like i called mines net.ildella.archetypes.base and net.ildella.archetypes.pheadra.
  2. Modify the pom.xml in src/main/resources/archetype-resources. This will be the one that will be used as basis for generated projects. Put in there whatever you like: plugins with configurations, dependencies, some comment as tip or reminder should also be useful.

Then a simple "mvn install" will install archetype artifact in local repository.

Let's now go for the Catalogue: create a file called archetype-catalog.xml with a content like this one:


<?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog>
   <archetypes>
<archetype>
           <groupId>net.ildella.archetypes</groupId>
           <artifactId>base</artifactId>
           <version>1.0</version>
           <repository>http://www.mvnsearch.org/maven2</repository>
           <description>Basic maven projects with useful stuff configured</description>
       </archetype>
<archetype>
           <groupId>net.ildella.archetypes</groupId>
           <artifactId>phaedra</artifactId>
           <version>1.0</version>
           <repository>http://www.mvnsearch.org/maven2</repository>
           <description>Basic + pheadra dependencies and stuff</description>
       </archetype>
   </archetypes>
</archetype-catalog>

and put it in your ~/.m2/repository folder, or wherever you keep your repository.
That's all. Now we can do:

mvn archetype:generate -DarchetypeCatalog=file:///home/ildella/.m2/repository/

to get the following result:

We choose a number and we got our skeleton project.

If like me you started from wicketrad-archetype, have a look at file src/main/resources/META-INF/maven/archetype.xml to see how to include resources to your skeleton project, for example to add some demonstration classes.

 

Filed under  //   maven  

Comments [0]

Use update-alternatives to upgrade maven installation on Ubuntu.

Use debian update-alternatives to easily swith from different version of the same program you have installed on your machine. Or, more frequently, just to switch to a new version in a fast and clean way.
Let's make a simple example with maven. Ubuntu repositories have an old version and I want to use the new one with the same comfort of the packaged one. Here we go.

Let's download and unzip the new maven distribution in your $HOME/software/maven2.2.1 or wherever else you like.
Now, from a terminal:

# sudo update-alternatives --config mvn

you will see what you have installed and configured and what is the actual default version (the one with the *)

to install the new version:

# sudo update-alternatives --install /usr/bin/mvn mvn /home/ildella/software/apache-maven-2.2.1/bin/mvn 1

now again:

# sudo update-alternatives --config mvn

you will see another options, choose the right number and that's it! From now on, the "mvn" command will be binded to the 2.2.1 version of the program.
Here is the screenshot of my whole terminal session.

Filed under  //   maven   tools  

Comments [0]