Wiki Blog Gallery

Maven

Basic setup of Maven

  1. Download and install Maven+JDK as described here.
  2. Tune system variables. You need to create JAVA_HOME and M2_HOME variables and append the path to maven's bin directory to your path, as shown on below screenshot:
  3. Maven needs the initial configuration to setup HTTP proxy (if you have it in your office). The following configuration file1) should be placed to C:\Documents and Settings\<yourID>\.m2 directory:

    settings.xml

    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                          http://maven.apache.org/xsd/settings-1.0.0.xsd">
     
        <proxies>
            <proxy>
                <id>mylocalproxy</id>
                <active>true</active>
                <protocol>http</protocol>
                <host>proxy</host>
                <port>8080</port>
                <!-- <nonProxyHosts>
    *.google.com|ibiblio.org</nonProxyHosts> -->
            </proxy>
        </proxies>
     
    </settings>
  4. You need to deploy all libraries, which are missed in public Maven repositories:

    deploy.xml

    call mvn install:install-file -Dversion=0.1-SNAPSHOT -Dpackaging=jar -DgroupId=org.company.package -DartifactId=library-name -Dfile=library.jar
  5. Checkout the projects needed, better using Eclipse .psf file.
  6. Run the following commands:
    • To build the complete project and create a packaged ZIP file with editor:
      mvn install package
      As the result the following package is build, which is a packaged application: project_folder\target\project.zip
    • To generate Eclipse classpaths from POMs run:
      mvn eclipse:eclipse
    • For the quick install (no tests and no updates from remote repositories) run:
      mvn -Dmaven.test.skip=true -o install

Note: Unfortunately, it is hardly possible to make an assembly in nn_build project (see 1, 2, 3). So the assembly is done in a project, which contains the main class. This allows also the assembly to be bound to «package» goal.

Q: I want to install WAR file together with JAR file, i.e. how can I make maven to create and install two artifacts with different packaging?

A: If you have a war packaging in your pom.xml and you want to create a JAR file togуther with WAR file, try this command: mvn -o jar:jar install:install-file -Dfile=${project.build.directory}\${project.artifactId}-${project.version}.jar -Dpackaging=jar -DpomFile=pom.xml. Also using profiles like in Building For Different Environments with Maven 2 may help.

Releasing the project

For those, who want to make a release from a command line, I would refer them to look at maven-release-plugin (see good reference here). Here we will setup the following infrastructure:

  • Using Nexus as Maven repository manager2)
  • Using Hudson as continuous integration system.

So, what you need to do:

  • Make sure, that you have installed Hudson Maven Release Plug-in in Hudson plugin manager.
  • You have to enable plugin for specific project in project settings. In case you access SVN repository anonymously, you may wish to specify username/password pair for maven-release-plugin as well as tag base URL -Dresume=false release:prepare release:perform -DtagBase=https://localserver.com/svn/public/tags/java/projects/ -Dusername=john -Dpassword=nnn.
  • maven-release-plugin will use project.scm.developerConnection (or project.scm.connection if the last one is not defined) to commit to source repository and project.distributionManagement for artifact deployment. So your pom.xml may look like this:

    pom.xml

    <project>
        <modelVersion>4.0.0</modelVersion>
     
        <groupId>org.erasmusmc.rdf-storage</groupId>
        <artifactId>rdf-storage-service</artifactId>
        <packaging>war</packaging>
        <version>1.0-SNAPSHOT</version>
     
        <name>My Service</name>
     
        <scm>
            <connection>scm:svn:https://localserver.com/svn/trunk/java/projects/myservice</connection>
        </scm>
     
        <distributionManagement>
            <repository>
                <id>nexus-server-id</id>
                <url>http://mavenserver.com/nexus/content/repositories/releases</url>
            </repository>
            <snapshotRepository>
                <id>nexus-server-id</id>
                <url>http://mavenserver.com/nexus/content/repositories/snapshots</url>
            </snapshotRepository>
        </distributionManagement>
     
        ...
    </project>
  • After that you can make a release from Hudson project menu

Some helpful information:

Concerning multi-project release:

Q: Where I can get more detailed explanation about POM?

A: See here.

Q: mvn release:prepare fails with message svn: OPTIONS of Server certificate verification failed: issuer is not trusted

A: You need to run the svn list ... command manually under the specified user (possibly, changing the user via sudo -u host_user -H /bin/bash3) ), and then accept the server's certificate permanently4).

Q: mvn release:prepare fails with message svn: '/tags/java/project' path not found.

A: This happens because plugin does not create parent path entries when tagging. See release:prepare should create the SVN tags parent dir if it doesn't already exist for more details.

Q: How to configure several source directories for maven project? A: This can be achieved using build-helper-maven-plugin like below:

pom.xml

<project>
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>org.mycompany</groupId>
    <artifactId>project</artifactId>
    <packaging>jar</packaging>
    <version>1.1</version>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${basedir}/import</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Q: How to share resources between maven projects?

A: See here and here. The example below shows how to copy resources from supplier-project to consumer-project/target/addon folder:

pom.xml

<project>
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>org.mycompany</groupId>
    <artifactId>consumer-project</artifactId>
    <packaging>jar</packaging>
    <version>1.1</version>
 
    <dependencies>
        <dependency>
            <groupId>org.mycompany.common</groupId>
            <artifactId>supplier-project</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <!-- Copy shared resources: -->
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/addon</outputDirectory>
                            <includeGroupIds>org.mycompany.common</includeGroupIds>
                            <includeArtifacIds>supplier-project</includeArtifacIds>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Q: How to share test code between projects?

A: For the test-common project add to your pom and then use the resulting jar artefact with tests classifier in test scope:

pom.xml

<project ...>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                     <execution>
                         <goals>
                             <goal>test-jar</goal>
                         </goals>
                     </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Q: What is the build phases sequence (lifecycle)?

A: See here.

Q: How to exclude dependencies from dependent tree?

A: To view the dependency tree use mvn dependency:tree. And see here.

Q: How to define Java heap space for maven?

A: MAVEN_OPTS=-Xmx512m

Q: How to deploy .jar artifact next to .war artifact when pom packaging is war?

A: Use mvn jar:jar install:install-file -Dfile=${project.build.directory}\${project.artifactId}-${project.version}.jar -Dpackaging=jar

Q: Where can I get more information about maven?

A: Read Maven Book: The Definitive Guide.

1) See here the complete list of options for settings.xml
3) -H option is important, otherwise authentication information will be persisted in sombody's else ~/.subversion/ folder
4) See also here, here and here more discussions on authentication topics for maven-release-plugin
 
software/maven.txt · Последние изменения: 2009/10/16 12:01 dmitry
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki