Showing posts with label maven. Show all posts
Showing posts with label maven. Show all posts

Tuesday, June 5, 2012

Setting up and accessing a remote maven repository

Now that we've reviewed the basics for maven it's time to explore one of the more common situations in a maven project, setting up and accessing a remote repository. For any team greater than one person (which I'm guessing covers a slim portion of projects out there) and projects with more than a single executable (I'm guessing there is one or two of these types out there) you'll need a place to build the building blocks for more advanced projects (such as a utility package or a database entity project).

Creating a remote repository

While many people will say finding a place to store your repository would be the first thing you should consider I say that is the second thing you should do, the first thing is decide how you want users to access the repository and how you want contributors to upload content to the repository. Do you want to make it available over http? Will you need to use the SCP protocol to upload content? Are all your machines the same (Windows vs Linux vs Mac) and only internal access is necessary so a file:// protocol would work? These all will make a difference where you put the repository. After these questions are answered you can pick a server that fits these parameters.

Adding a deployment destination to a project

Now that you've decided where to place your project you'll need a way to add it to your project. Deployment locations are handled in the distributionManagement section of the pom file. It goes at the end of the file after the dependencies section and a sample distribution section appears below:
<distributionManagement>
  <repository>
    <id>serverId</id>
    <name>Example Upload Location</name>
    <url>scp://server/srv/maven/repository</url>
  </repository>
</distributionManagement>
Pretty simple right?  There are really two important parts of this snippet, first is the id of the server and second is the URL. The URL's significance should be obvious so I'll focus on the id field. The reason the id field is important is because that is the link between the pom file and this server setup and the personal field setup for this server in the settings.xml file. Depending on what type of URL you use, such as scp, you need additional information, like authentication and directory/file permissions, to perform the task so without further ado lets look at:

Settings.xml

Up until now we haven't had to worry about maven's other configuration file as nearly all tasks handled through the pom file don't need any additional configuration. Technically, if you were to use a file protocol for the deployment you still wouldn't need to configure a server in settings.xml but I have yet to see a system that could use that effectively. While you can configure many different things, such as a proxy or different profiles, through the settings.xml file the only major one that most people will need to use is the servers section. However, if you want to to learn more about the settings.xml file you can do so here. The entire file isn't all that big so reviewing it is quick. For a final note about the settings.xml file is where to find it; the file is located in each user's home directory (on linux it would usually be at /home/<userName>/.m2 and in windows usually C:\Users\<userName>\.m2, your mileage may vary).

servers

The important part of the settings file for distribution management is the server section. In this section you can define personal information about connecting to various servers. In the pom file you define the URL (some would ask why this wouldn't also be here but you can use the same server for multiple functions so the URL needs to be in the pom for different setups) but the rest of the information is here in settings (I'm guessing you may not want your user information broadcast to the rest of the world). A typical server setup looks like the following:
<servers>
  <server>
    <id>serverId</id>
    <username>my_login</username>
    <password>my_password</password>
    <privateKey>${user.home}/.ssh/id_dsa</privateKey>
    <passphrase>some_passphrase</passphrase>
    <filePermissions>664</filePermissions>
    <directoryPermissions>775</directoryPermissions>
  </server>
</servers>
First, if you go back up to our distributionManagement example you'll notice that the id fields match. That would tell maven to use the credential and permissions here when uploading content to the remote repository. You should either enter a password here or is private key/passphrase entry. You should not use both. The permissions are used for the directories and files created on the remote system. If you need an explanation on what the numbers mean you can search for unix file permissions or click here.

A quick bit on security

Starting with Maven 2.1 you can encrypt your password so even if someone managed to get a hold of your settings file (I'm assuming here that you don't give access to this to anyone other than yourself) they still couldn't see your password. Plus, administrators always get nervous when passwords are available in plane sight. If you want/need to encrypt your password I would just follow the instructions on the maven site to set it up here.

Adding a remote repository to a project

There are a plethora of reasons you may need to add a repository to your project.
  • Your company has a policy that prohibits downloading software packages directly from the web so you need to connect to a mirrored main repository inside the company
  • You need to connect to a third party vendor for additional software packages
  • You need to connect to an internal repository to use internal software APIs
Whatever the reason, there is a simple way to connect a maven project to one of these repositories. First, you need to add a repository to the repositories section of the pom (just before the dependencies section. The entry would look like the following:
<repositories>
  <repository>
    <id>java.net.maven1</id>
    <name>java.net</name>
    <url>http://download.java.net/maven/2</url>
   </repository>
</repositories>
Like the distribution management element above the name is just a name you want to see. For the id, it only matters if you need additional authentication information for the server you're connecting to. Since most access is over http and unauthenticated it usually doesn't matter. However, if you needed to lock down even the pull and ended up using something like scp then you'd want to make sure the id matches a server defined in settings.xml (see above).

Thursday, May 10, 2012

Getting started with Maven

What is Maven

Maven is quickly becoming, if it isn't already, the most popular build tool for java projects. With plugins you can do pretty much anything but at its heart Maven is a project management tool that handles dependency management, building and testing a java project. By default it can handle all the default java project types, jar, war, ejb-jar and ear.
The purpose of this article is to get familiar with the Maven setup so items like plugins and site development won't be covered here. There will be other articles that will cover those topics.

Application Setup

"Installing" Maven consists of downloading the package from the Maven site and unzipping the contents into a directory. If you're using an IDE such as Netbeans, Intellij or Eclipse you can usually skip this step as they already have an install built into the IDE. The important file for command line execution is the mvn.bat (windows) or mvn (linux) file in the bin/ directory. This file starts the maven process. Additional instructions and specialty builds of Maven are available on the Maven download page.

pom.xml

Aside from the application install the only other required file for maven is the pom.xml file. The pom file is the basis for a specific Maven project (such as a jar or a war). The pom file also contains information about other projects this project depends on (dependencies) the version to be built and deployment information.

Build information

At the start of the pom file is information about the name, version and type of the artifact to build. This information is summed up in four elements:
  1. groupId--this is like a package for your project, usually in the form com.<company name>.<group area> such as com.tigereye.examples
  2. artifactId--This is usually the name of the project
  3. version
  4. packaging--this tells Maven what type of artifact the project is. (Such as jar or war, it also tells maven how to package the artifact and any additional steps necessary for the specific project type)

Dependencies

While not strictly required for a project only the most basic projects don't depend on external sources. Whether it's hibernate for database access, the JEE API files for building an enterprise applications or an internal utility package pretty much all but the most basic applications require some kind of external resource to function.
Personally, this is where I believe Maven proves its worth. When setting up your project you only need to define programs your program directly depends on. Maven handles pulling in all the dependencies that project depends on that you don't so you don't have to worry about it. Let's say that you need to use hibernate but you don't specifically need to use antlr, you just have to add the dependency for hibernate and Maven will pick up antlr when you need to use hibernate (such as building a war or an ear). This way, you can focus on what your project needs and not worry about the rest of the jars necessary to actually run, Maven will ensure you have everything you need when it comes time to use them.
The dependency section starts with a dependencies element with one or more dependency elements. The content of each of the dependency element is as follows:
  1. groupId -- analogous to the groupId at the top of the file except this is the group id of the artifact you want to import
  2. artifactId -- like groupId, this is the artifact id of the artifact you want to use
  3. version -- the version of the artifact you want to use
  4. scope -- an optional attribute (defaults to compile) that has one of four values:
    • compile -- this package is necessary for compiling your code and all other steps
    • runtime -- this package isn't directly referenced in the code but needed to properly run
    • test -- this package is necessary for the testing tree but not necessary for running the main project (useful for packages like junit or testng)
    • provided -- this package is necessary to compile the project but will be provided for running the application later (such as servlet-api when writing a web app since it's provided by the web container)

Sample File

Below is a sample of a very basic pom file.
<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.psc.example</groupId>
  <artifactId>jax-ws</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>jax-ws Maven Webapp</name>
  <dependencies>
    <dependency>
      <groupId>javax.persistence</groupId>
      <artifactId>jpa-api</artifactId>
      <version>2.1</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>3.6.7.Final</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.sun.faces</groupId>
      <artifactId>jsf-api</artifactId>
      <version>2.1.1-b02</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
</project>
The above sample is a basic web project. It uses JPA for its persistence which requires a compile time dependency with hibernate as the implementing persistence framework. Since we're using JPA in our code the hibernate dependency is specified as runtime (it won't appear on the compile classpath but it will be included in the WAR. We also have the JSF api package that is necessary for the presentation pages but that package is already a part of the container so we mark it as provided (it's used for compiling but won't be included in the WAR). Finally, for testing we need junit but we don't want it on the compile classpath or in the WAR so we mark it as test.

Project Structure

While you can override the folders for things like source if you want, unless there's a good reason it just makes good sense, not to mention a smaller pom file, to use the default directory layout for your project. The standard setup for files is as follows:
<project directory>
    src
        main
        java
        resources
        webapp (if your app is a web-app)
    test
        java
        resources
    pom.xml
If you aren't using your IDE to generate the project structure and you don't want to hand code the directory names you can execute the following maven script in the directory ABOVE the project directory:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Substitute groupId and artifactId with appropriate names for your project. After it finishes executing a directory matching your artifactId.
Once you start compiling classes and running tests one more directory will be created by maven for storing the compiled classes called target. This directory is at the same level as src and will have sub directories under it for the compiled classes, web content and test resources, just like src has.

Execution

Once you have the rest of the maven setup complete you'll need to be able to perform actions on your project. In maven speak an action is called a phase. Maven has a very rich set of phases you can perform. In keeping with the theme of this article I'm only going to go over the basic ones here to get you up and running but the linked site has all of them and examples of what they do and how to use them correctly.
The main phases you'll be needed for day-to-day use are:
  • compile -- This compiles all source java files and copies any resources to the target directory
  • test -- runs all tests found in the system (if you add -DskipTests it will compile the tests but not run them or -Dmaven.test.skip=true to not even compile them. I'm sure I don't need to tell you how dangerous this can be)
  • package -- builds the output file (jar for standard projects, war, ear, ejb jars, etc. for other project types)
  • install -- copies the package to your local maven repository.
One thing to note, each phase builds on the previous one so if you execute the package phase it will execute compile, test and any other defined phase before the package phase. This becomes important as you move down the phase chain since a failure in a previous phase will halt the execution. 

Local Repository

Finally, while not something neophytes to maven will care about knowing about your local repository will help understand some features of maven. The idea of your local repository is a place on your machine where dependent packages (both for your projects and for maven itself) can be stored so yo don't have to go out to the internet each time you need to check something. In fact, the first time you run maven and, in general, the first time you use a new package you'll most likely notice a slowdown in the initial phases as maven downloads updated or new dependencies. Under your home directory (Windows: C:\Users\<username> or linux: /home/<username>) is a .m2 directory. Under there is the repository. It's essentially a network of first group ids, then artifact ids and finally version number directories where jars, poms and additional information for maven  are stored for use by both maven and you projects. As a general rule (even for more advanced things) you shouldn't need to mess with this much other than occasionally removing entries.

Conclusion

I can hear you now, "but what about the settings.xml file?", "what about plugins?", "what about deployments?" and any number of other things that maven can do. While I do agree that they're all important they really aren't necessary for getting started with maven and setting up an initial maven project. I'm sure I'll be covering them in additional posts but if you don't have a good foundation then the rest will fall apart.