Archive for the ‘Maven’ Category

Articles

Maven – Debug Test

In Debug,Maven,Test on 17/03/2011 by pier0w

So if you happen to program in Intellij IDEA like my self you may have come across a situation where your tests pass within your IDE but fail when running mvn clean install. Generally this is because you did something silly in one of your tests that is now infecting all the following tests.

Now it’s all well and good knowing you’ve done something wrong, but it’s not so helpful if you don’t actually know WHAT that something is. So now you’ll be thinking “I KNOW! I’ll run mvn clean install within Intellij IDEA in debug mode then step through them there tests. I SO SMART!”. Then with your maven build profile all configured within Intellij and your break points all precisely set you click on run debug and… the tests run to the end without a pause in site.

Now is when you’re wondering what the duce just happened. Well it turns out that maven runs it’s test in forked processes, this means that you may be happily debugging the maven process but you have absolutely no connection to the forked processes that are running the tests. So what to do?

The first, easiest and maybe even best thing to do is add the -DforkMode=never parameter to your mvn clean install command to become…

mvn clean install -DforkMode=never

Who’d of thought?

In regards to your profile, put it in the Goals text field.

This will stop maven forking the tests so they will stay within the maven process you are actually debugging. So problem solved.

Though, what if you want to debug some tests that are running on another server? Well that is actually very simple as well. Simple kick the tests off with the -Dmaven.surefire.debug parameter included within your command. The maven tests will now pause at the beginning and expose a debug socket on port 5005 where you can then connect your favourite IDE to carry out the debugging. This works weather the tests are forked or not.

mvn clean install -Dmaven.surefire.debug

And that’s it, now you know how to debug your maven tests no matter where they are.

Articles

Maven New Project Tutorial

In Maven,Tutorial on 12/02/2011 by pier0w

Maven is a Java build tool that not only compiles your project and automatically runs all it’s tests it also provides a really good directory structure and handles the retrieval of any JAR dependencies your project might have. Also as you might have guessed it builds your project in a way that it is then able to be used as a maven dependency it’s self.

This tutorial is only going to describe the very basic process of creating a new maven project. So lets begin.

Creating a maven project is as simple as running the following command.

#> mvn archetype:generate
...
365: remote -> wicket-scala-archetype (-)
366: remote -> wikbook.archetype (-)
367: remote -> circumflex-archetype (-)
368: remote -> javg-minimal-archetype (-)
Choose a number: 99:

Pro tip: If you get an error like the following saying that maven can’t find the archetype plugin this means that your repository is set to download a version of the plugin that is too new for the repository that you are using.
[INFO] Unable to find resource 'org.apache.maven.plugins:maven-archetype-plugin:maven-plugin:2.0-alpha-6' in repository central (http://repo1.maven.org/maven2)

To fix this you will need to open up the .m2/repository/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata-central.xml file and edit the <latest> and <release> tags. Place the value of one of the <version> tags within them that is a couple version down from the what is in there. It’s also a good idea to just stay away from alpha versions so maybe take the highest version that isn’t alpha. Your maven-metadata-central.xml file will then look as follows.

<?xml version="1.0" encoding="UTF-8"?>
<metadata>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-archetype-plugin</artifactId>
  <versioning>
    <latest>2.0</latest>
    <release>2.0</release>
    <versions>
      <version>1.0-alpha-3</version>
      <version>1.0-alpha-4</version>
      <version>1.0-alpha-7</version>
      <version>2.0-alpha-1</version>
      <version>2.0-alpha-2</version>
      <version>2.0-alpha-3</version>
      <version>2.0-alpha-4</version>
      <version>2.0-alpha-5</version>
      <version>2.0</version>
    </versions>
    <lastUpdated>20101028011818</lastUpdated>
  </versioning>
</metadata>

This technique can also be used to fix any other plugins that might not download.

The mvn archetype:generate command will kick off a process to allow you to easily generate a maven project. You’ll be displayed with a list of around 300 – 400 custom maven projects that provide you with good starting points for different situations and frameworks, read through them, you might find a good starting point for your project, otherwise just select the default option (99: remote -> maven-archetype-quickstart) by pressing ENTER.

367: remote -> circumflex-archetype (-)
368: remote -> javg-minimal-archetype (-)
Choose a number: 99: 
Choose version: 
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6:

Next you will be asked which version of the template you would like to use, select the latest by pressing ENTER again.

Now you need to select how you want to name your project by giving the group id (namespace), artifactId (name), version, and package name (generally but not always the same as your given groupId) of your project. Fill these in with what ever you feel is apropriate.

5: 1.0
6: 1.1
Choose a number: 6: 
Define value for property 'groupId': : org.project 
Define value for property 'artifactId': : project
Define value for property 'version': 1.0-SNAPSHOT: 1.0
Define value for property 'package': org.project: #Press ENTER to select the groupId as the package name by default
Confirm properties configuration:
groupId: org.project
artifactId: project
version: 1.0
package: org.project
Y: #Press ENTER to confirm

You will now have a brand new maven project.

#> ls -R project/
project/:
pom.xml  src

project/src:
main  test

project/src/main:
java

project/src/main/java:
org

project/src/main/java/org:
project

project/src/main/java/org/project:
App.java

project/src/test:
java

project/src/test/java:
org

project/src/test/java/org:
project

project/src/test/java/org/project:
AppTest.java

That is it you can now begin developing within the maven project. To build and run tests use the following command.
#> mvn clean install

If the build command finishes successfully maven will place a copy of the JAR file it has built from your project into your local maven repository (the place where maven keeps all of your dependencies). This means that any classes from your project will be accessible from any other local maven projects as long as you add your project as a dependency. Your should remember though that your local build JAR’s only ever get updated after a successful run of mvn install so if you make a change in one project and want it to be seen by your other projects you will have to run a build.

Articles

How to build specific maven projects

In Maven on 04/03/2010 by pier0w Tagged: ,

The latests versions of maven allow you to select which projects you would like to build when running from the root projects.
So say we have the following project structure:


A / AA
A / AB
B / BA
B / BB
C / CA
C / CB

If we wished to run a “clean install” only over the projects ‘A’, ‘BB’, and ‘CA’ we can do this with the “-pl, –projects” option.
Though when this option is used it acts as a whitelist so if we wish to build the whole of ‘A’ including ‘AA’ and ‘AB’ then we will have to list them as well. Assuming this is the case then the command would be as follows:

mvn clean install --projects A,A/AA,A/AB,B/BB,C/CA

Now the names used in this option are the directory names of the projects NOT the artifactId’s within the pom files.

Comments Off on How to build specific maven projects