Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

Sunday, August 12, 2007

Maven Download Sources

When using maven, it normally downloads only the binaries from the dependencies your project has.
It is possible, with simple commands, to also download the source and/or documentation from the dependencies you are interested in.

To download sources:

mvn [goal] -DdownloadSources=true


To download javadocs:
mvn [goal] -DdownloadJavadocs=true

Maven Eclipse

The maven eclipse plugin is provided for those who have a ready .pom file for a maven project and want to start using eclipse as the IDE for the project.

The command is as simple as running:

mvn eclipse:eclipse

This command have a number of parameters, but one of the most interesting ones would be the one which allows to create a WTP (Web Tools Platform) aware eclipse project.

mvn eclipse:eclipse -Dwtpversion=1.0

This command will create an eclipse project with WTP 1.0 support.

Friday, August 3, 2007

Adding Repositories to Maven 2

Maven uses on-line repositories to find the dependencies you set in your pom file. By default, maven uses the repository specified in the

If you want to use other repositories, you are able to do so by adding a ~/.m2/settings.xml file. This file should be provided in the following format:


<settings>
<profiles>
<profile>
<id>standard-extra-repos</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>jboss</id>
<url>http://repository.jboss.com/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>jboss-snapshot</id>
<url>http://snapshots.jboss.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>jboss-plugins</id>
<url>http://repository.jboss.com/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>jboss-snapshot-plugins</id>
<url>http://snapshots.jboss.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
</settings>


Some repositories I've collected and you might be interested in using, because some artifacts are available only through some of these repositories and not all of them. It is important to remember that the more repositories you add, the slower will be your build, so use them with wisely.

Repository
http://www.ibiblio.org/
http://download.java.net/maven/2
http://repo1.maven.org/maven2
http://repository.jboss.com/maven2
http://maven.apache.org/
http://mojo.codehaus.org/


Snapshot
http://snapshots.jboss.org/maven2
http://people.apache.org/repo/m2-snapshot-repository
http://snapshots.maven.codehaus.org/maven2
http://snapshots.repository.codehaus.org


If you want to download snapshot version of maven plugins, there is a page which explains how to setup your settings.xml file.

Sunday, July 15, 2007

Choose Java Version for Maven compile

It is possible to set the version of the jdk one wants to use when compiling code using maven.

This might be mandatory if your code uses generics or annotations. So here is the code you should add to your pom.xml.


<build>
...
 <plugins>
...
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.5</source>
     <target>1.5</target>
    </configuration>
   </plugin>
 </plugins>
...
</build>
...

Friday, June 8, 2007

Build Struts 2 Application Using Maven 2

I really think that using Maven as tool to build Java Projects is simply great, but if there is one thing I hate is searching ibiblio for the dependencies I need for my project. So I will post below the path for the main components one will need to get started with Struts 2 on his project.

By default, Maven will look for the dependencies on its central repository http://repo1.maven.org/maven2. It is possible to configure the repositories where you want maven to look up for files. These two below are some alternative repositories where you can try to find components you need. In the apache repository below is possible to find the SNAPSHOT version of the components.

Just add the code below into your pom file.


<repositories>
  <repository>
    <id>ibiblio</id>
    <name>iBiblio Maven2 Repository</name>
    <url>http://www.ibiblio.org/maven2</url>
  </repository>
  <repository>
    <id>apache-repo</id>
    <name>Apache Repository</name>
    <url>http://people.apache.org/repo/m2-snapshot-repository</url>
  </repository>
</repositories>


Check the official Maven site about how to setup multiple repositories.

Now, we can reach for those components! :)
As advised by the struts team, the minimum set of components one might need is below.

  • struts2-core.jar

  • xwork.jar

  • ognl.jar

  • freemarker.jar

  • commons-logging.jar


Fortunately, to download all of them automagically, all you need to add to your pom files are the lines below:


  <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.0.6</version>
  </dependency>


Wanna do this even faster? Use maven archetypes to create a struts2-aware project! ;)


mvn archetype:create -DgroupId=tutorial \
    -DartifactId=tutorial \
    -DarchetypeGroupId=org.apache.struts \
    -DarchetypeArtifactId=struts2-archetype-starter \
    -DarchetypeVersion=2.0.5-SNAPSHOT \
    -DremoteRepositories=http://people.apache.org/repo/m2-snapshot-repository

Monday, April 23, 2007

Quiclky Create a Maven-aware project

If you are (or want to start) using Maven as a build tool for your project, a quick way to start is to use an archetype to create a pom.xml and the default folder structure. This is the command:


mvn archetype:create

-DarchetypeGroupId=[archetype-groupId]

-DarchetypeArtifactId=[archetype-artifactId]

-DarchetypeVersion=[archetype-version]

-DgroupId=[my.groupid]

-DartifactId=[my-artifactId]

Monday, February 26, 2007

Install a jar file into maven's local repository

Sometimes, while using maven to build your projects, it is necessary to add a needed jar file directly into your local repository. This should not be a need if you have a remote repository correctly setup, but it might be useful if it is a too bureaucratic task or any other reason.

It is as simple as this. Having the file to be installed in your local repository in the local folder, issue the command below to install it.

mvn install:install-file -DgroupId=<MVN_GROUPID> -DartifactId=<MVN_ARTIFACTID> -Dversion=<MVN_VERSION> -Dpackaging=jar -Dfile=<JAR_FILE_TO_INSTALL>

Could be useful! ;)

Sunday, February 11, 2007

Maven Profiles

I was struggling with some different configuration files for my development and production environment. As a "weekend-free-time-developer-now-that-I-changed-into-management", my development environment is restricted. So I use the same instance of MySQL for the production database and run some tests on the same server with a different schema and configuration.

Using maven as a build/management tool improved my a lot my performance, but these different environments still gave me some headache. So I decided to leave laziness aside and configure some profiles in my pom files.

Maven profiles allow you to prepare different sets of configuration. There are for different types of profiles, defined in different files and with different purposes:

  1. Per Project: Defined in the POM itself (pom.xml).
  2. Per User: Defined in the Maven-settings (%USER_HOME%/.m2/settings.xml).
  3. Global: Defined in the global maven-settings (%M2_HOME%/conf/settings.xml).
  4. Profile descriptor: a descriptor located in project basedir (profiles.xml)


I decided to use the type number 1, which is a Per-Project configuration type, and unlike number 4 (which has a restricted scope) which is also a Per-Project configuration type it has the ability to change a lot of the project settings because it is in the same pom file as the projects' specifications themselves.

For starters, I wanted to have different jboss' data source xml files for production and development environments. As Maven works with a hierarchy of configuration files I figures the less intrusive way to set the build configuration was to have different folders for each environment. So where I previously had /src/main/resources/WEB-INF now I would have /src/main/resources/development/WEB-INF and /src/main/resources/production/WEB-INF. With the original single path, Maven did not need any special configuration to find my data source xml files (I love "convention over configuration"). With the 2 new folders, I had to add another layer to the hierarchy of Maven's configuration files and this layer would be the profiles.

In my pom.xml, I added a profiles section, obviously enough marked by the tag <profiles>. Here is what I added to my original pom.xml.
  <profiles>
    <profile>
      <id>env-dev</id>
      <build>
        <resources>
          <resource>
            <directory>src/main/resources/development</directory>
          </resource>
        </resources>
      </build>
      <activation>
        <property>
          <name>env-dev</name>
        </property>
      </activation>
    </profile>

    <profile>
      <id>env-prod</id>
      <build>
        <resources>
          <resource>
            <directory>src/main/resources/production</directory>
          </resource>
        </resources>
      </build>
      <activation>
        <property>
          <name>env-prod</name>
        </property>
      </activation>
    </profile>
        
  </profiles>

Now, you can tell maven to build based on different profiles. Where you normally would enter a command like mvn install now you should change to:

mvn install -P env-dev for development environment

OR

mvn install -P env-prod for production environment

After doing this, I was wondering... Which profile will maven choose to execute the build if I do not explicitly tell it?

That is very easy to check! Run the command:
mvn help:active-profiles

It will show you that there are no active profiles, but it will (wrongly IMHO) add BOTH (production and development) folders to your final build, and will not realize that those are actual resource folders.

One simple solution for this issue is to tell maven which profile should be used by default. I modified the profiles tag content to use the production environment as the default one, so this will force me during development to always add the "-P env-dev" while I am testing/developing and this should make me used to always tell maven what I really want. The way to tell maven which one is the default active profile is in bold below.

    <profile>
      <id>env-prod</id>
      <build>
        <resources>
          <resource>
            <directory>src/main/resources/production</directory>
          </resource>
        </resources>
      </build>
      <activation>
        <activeByDefault>true</activeByDefault>
        <property>
          <name>env-prod</name>
        </property>
      </activation>
    </profile>

Now, if you run mvn help:active-profiles again you will see that the env-prod is being used, which is the same as running mvn help:active-profiles -P env-prod.

$ mvn help:active-profiles
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'help'.
[INFO] ----------------------------------------------------------------------------
[INFO] Building Fisio EJB Component
[INFO] task-segment: [help:active-profiles] (aggregator-style)
[INFO] ----------------------------------------------------------------------------
[INFO] [help:active-profiles]
[INFO]
Active Profiles for Project 'com.jc.fisio:fisio-ejb:ejb:0.1-SNAPSHOT':

The following profiles are active:

- env-prod (source: pom)



[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Sun Feb 11 18:13:13 BRST 2007
[INFO] Final Memory: 2M/5M
[INFO] ------------------------------------------------------------------------

Working with profiles is quite interesting, because it allows you to have multiple active profiles at the same time, providing a number of different combinations. It can be used to may purposes, like setting different application servers, web servers, snapshot version etc.

Saturday, January 27, 2007

Tomcat Hot Deployment in Windows

The post about hot deploying an application to Tomcat using maven brought me a problem: the struts.jar was always left in the WEB-INF/lib of the exploded directory in the server.

This is a know issue with Tomcat working under Windows and I found an article which I quoted here below with the solution for this problem:

If you try to hot-deploy a WAR file to Tomcat on Windows, you often encounter file locking issues. Windows won’t let Tomcat undeploy the old app because files are locked by the OS. Here is a fix:

Edit %CATALINA_HOME%\conf\context.xml. Find the root <context> and add these two attributes:

<context antijarlocking="true" antiresourcelocking="true">

Now you can copy updated WAR files to your deploy directory and Tomcat will remove the old app and hot-deploy your new app. I’m using this on Tomcat 5.5.11 without any trouble.

There were other people having this kind of problem. Please note that this has nothing to do with using Maven, but with the combination of Tomcat 5.5 + Windows and Hot Deploys! :)

Using Maven to deploy application to Apache Tomcat Server

For those who are not familiar with Maven, it is (as stated in the project site) a "software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information."

Based on the concept of "convention over configuration" developers may use Maven to compile, build, test, deploy, generate reports and use a whole bunch of other features to manage their software development project. In another oppportunity I will post an article about Maven, for now I will write about my most immediate need: deploy a war file to a tomcat server.

As I am using maven to manage my project from test to build, all I need is to configure the Cargo plugin. Cargo is able to download, install and start Tomcat, but this is not our focus right now, so I will assume you have Tomcat installed and running with no problems.

Using Maven´s Cargo plugin it is possible to install an application into Tomcat in two ways:

  1. Use the Tomcat Manager application, which is deployed by default with Tomcat 5.x and is nothing more than a web interface to manage your server. It has the ability to install a WAR into the server from a remote location.
  2. Copy the WAR File of your application directly into the File System where Tomcat is expecting applications to be, normally %CATALINE_HOME%/webapps.
As one can find strong and weak points in each of these options, I will tell a little bit about the first one.

Using the Tomcat Manager

Now we will deploy an application to Tomcat while it is still running. This is called Hot Deploying.

You should add some comands to your pom file, and they should look like the excerpt of code below. Actually it should be positioned between the tags:
<build>
<plugins>
...
</plugins>
</build>

<plugin>
<groupid>org.codehaus.cargo</groupid>
<artifactid>cargo-maven2-plugin</artifactid>

<configuration>

<!-- Container configuration -->
<container>
<containerid>tomcat5x</containerid>
<type>remote</type>
</container>

<!-- Configuration to use with the Container -->
<configuration>
<type>runtime</type>
<properties>
<cargo.tomcat.manager.url>http://localhost:8080/manager</cargo.tomcat.manager.url>
<cargo.remote.username>admin</cargo.remote.username>
<cargo.remote.password>admin</cargo.remote.password>
</properties>
</configuration>

<!-- Deployer configuration -->
<deployer>
<type>remote</type>
<deployables>
<deployable>
<groupid>com.company</groupid>
<artifactid>web-component</artifactid>
<type>war</type>
</deployable>
</deployables>
</deployer>

</configuration>

</plugin>
You should need to change only the values in the and tags with the values that describe your component WAR file you wanto to install to Tomcat.

Having this in your pom file, you need to issue the following command:

mvn cargo:deploy
If you already have the application running in tomcat it is necessary that you undeploy it before deploying. So, issue the command below and you should be ok:

mvn cargo:undeploy

You also have the option to execute a command that will do an undeploy before attempting to deploy:

mvn cargo:deployer-redeploy
That should be enough to get the application running in Tomcat, deploying from a remote server.

I would just like to add that the configuration for the tags
and are values that must match the ones in the tomcat-users.xml file with the role "manager". Check this before trying to do the deploy.