Maven2

From no name for this wiki
Revision as of 17:51, 25 March 2010 by Claude (talk | contribs) (Dependencies auflisten)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Einfaches 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>mom.mom.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.12</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>pefilereader</groupId>
      <artifactId>peartifact</artifactId>
      <version>1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-annotations</artifactId>
     <version>3.4.0.GA</version>                     
    </dependency>
    <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-entitymanager</artifactId>
       <version>3.4.0.GA</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>ejb3-persistence</artifactId>
      <version>1.0.2.GA</version>
    </dependency> 
  </dependencies>
  <build>
    <filters>      
    </filters>
    <resources>      
    </resources>
    <plugins>
    </plugins>
  </build>
</project>

Maven2 Befehle

Projekte erstellen

Einfaches Projekt:

mvn archetype:create \
 -DarchetypeGroupId=org.apache.maven.archetypes \
 -DgroupId=com.mycompany.app \
 -DartifactId=my-app

Webprojekt:

mvn archetype:create \
   -DarchetypeGroupId=org.apache.maven.archetypes \
   -DarchetypeArtifactId=maven-archetype-webapp \
   -DgroupId=com.mycompany.app \
   -DartifactId=my-webapp

Projekt kompilieren

mvn compile

Projekt testen

mvn test 

und

mvn test-compile

Projekt installieren

So kann man ein Projekt ins lokale Repository installieren

mvn install

Eclipse Projekt erstellen

Der folgende Befehl erstellt alle Eclipse Dateien (.project u.s.w.)

mvn eclipse:eclipse

jar ins lokale Repository kopieren

So kopiert man eine Jar Datei ins lokale Repository.

mvn install:install-file
 -Dfile=<path-to-file>
 -DgroupId=<group-id>
 -DartifactId=<artifact-id>
 -Dversion=<version>
 -Dpackaging=<packaging>
 -DgeneratePom=true

Hilfe über ein Plugin anzeigen

-U braucht es nur manchmal. Zeigt die Goals und Parameter eines Plugins an.

mvn -U help:describe -Dplugin=compiler  -Dfull

Javaprogramm ausführen

mvn -e exec:java -Dexec.mainClass=org.sonatype.mavenbook.App -Dexec.args="sdfds sdfds"

Dependencies auflisten

Mit folgendem Befehl werden alle Dependencies eines Projektes aufgelistet:

mvn dependency:resolve

oder als Baum:

mvn dependency:tree

Build ohne Test

mvn clean install -Dmaven.test.skip

Ein Jar mit allen Dependencies eingepackt erstellen

Folgendes merged alle Classfiles in ein Jar, inklusive den Classfiles in den abhängigen Jars. Im Pom.xml:

  <build>
    <plugins>
      <plugin>
      	<artifactId>maven-assembly-plugin</artifactId>  
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>

Goal:

mvn install assembly:assembly

J2EE dependencies

Sun Klassen sind wegen lizenztechnischen gründen nicht im offiziellen Maven Repository. Mit Geronimo hat man eine Lösung:

  <dependency>
      <groupId>org.apache.geronimo.specs</groupId>
      <artifactId>geronimo-servlet_2.4_spec</artifactId>
      <version>1.1.1</version>
      <scope>provided</scope>
    </dependency>

Jetty plugin

Mit dem Jetty plugin können WAR Projekte gleich ausprobiert werden:

  <build>
    <finalName>simple-webapp</finalName>
    <plugins>
      <plugin>
         <groupId>org.mortbay.jetty</groupId>
         <artifactId>maven-jetty-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

Mit dem Goal jetty:run wird Jetty mit der Webapplikation aufgestartet:

mvn jetty:run

Testfailures ignorieren

So kann man die Fehler in den JUnit Tests ignorieren:

  <build>
    <plugins>
      <plugin>
	<groupId>org.apache.maven.plugins</groupId>
      	<artifactId>maven-surefire-plugin</artifactId>  
        <configuration>
          <testFailureIgnore>true</testFailureIgnore>
        </configuration>
      </plugin>
    </plugins>
  </build>

Multimodule Project

Das Multiprojekt besteht aus einem Webmodul und einem Jarmodul.

Das Multiprojekt befindet sich im Verzeichnis X/maven2sample. pom.xml vom Multiprojekt:

<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>org.sonatype.mavenbook</groupId>
  <artifactId>simple-parent</artifactId>
  <packaging>pom</packaging>
  <version>1.0</version>
  <name>Parent Project Maven2</name>
  <url>http://maven.apache.org</url>
  
  <modules>
    <module>simple-weather</module>
    <module>simple-webapp</module>
  </modules>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>


Das Webprjekt befindet sich im Verzeichnis X/maven2sample/simple-webapp. pom.xml vom Webprojekt:

<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>org.sonatype.mavenbook</groupId>
  <artifactId>simple-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>simple-webapp Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <parent>
      <groupId>org.sonatype.mavenbook</groupId>
      <artifactId>simple-parent</artifactId>
      <version>1.0</version> 
  </parent>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.geronimo.specs</groupId>
      <artifactId>geronimo-servlet_2.4_spec</artifactId>
      <version>1.1.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.sonatype.mavenbook</groupId>
      <artifactId>simple-weather</artifactId>
      <version>1.0</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>simple-webapp</finalName>
    <plugins>
      <plugin>
         <groupId>org.mortbay.jetty</groupId>
         <artifactId>maven-jetty-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

Das Jar-Projekt befindet sich im Verzeichnis X/maven2sample/simple-weather. pom.xml vom Jarprojekt:

<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>org.sonatype.mavenbook</groupId>
  <artifactId>simple-weather</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>simple-weather</name>
  <url>http://maven.apache.org</url>

  <parent>
      <groupId>org.sonatype.mavenbook</groupId>
      <artifactId>simple-parent</artifactId>
      <version>1.0</version> 
  </parent>

  

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
   <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-entitymanager</artifactId>
       <version>3.4.0.GA</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>ejb3-persistence</artifactId>
      <version>1.0.2.GA</version>
    </dependency> 
  </dependencies>
</project>

EAR Projekt

EAR Projekte sind Teil eins Multimoduleprojekts. Diese EAR Projekte bestehen meistens nur aus dem pom.xml und ein paar Konfigurationsdateien wie Applicationserver spezifische Deskriptoren.

Anbei ein Beispiel eines EAR Projekts:

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <parent>
    <artifactId>simple-parent</artifactId>
    <groupId>org.sonatype.mavenbook</groupId>
    <version>1.0</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.sonatype.mavenbook</groupId>
  <artifactId>ear-app</artifactId>
  <name>ear-app</name>
  <version>1.0</version>
  <url>http://maven.apache.org</url>
  <packaging>ear</packaging>

  <dependencies>

    <dependency>
      <groupId>org.sonatype.mavenbook</groupId>
      <artifactId>simple-weather</artifactId>
      <version>1.0</version>
      <type>jar</type>
    </dependency>

    <dependency>
      <groupId>org.sonatype.mavenbook</groupId>
      <artifactId>simple-webapp</artifactId>
      <version>1.0</version>
      <type>war</type>
    </dependency>

    <dependency>
      <groupId>org.sonatype.mavenbook</groupId>
      <artifactId>ejbmodule</artifactId>
      <version>1.0</version>
      <type>ejb</type>
    </dependency>

  </dependencies>

  <build>
     <finalName>MyEnterpriseApp</finalName>
     <defaultGoal>package</defaultGoal>
     <plugins>
        <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-ear-plugin</artifactId>
           <configuration>
             <modules>
                <javaModule>
                   <groupId>org.sonatype.mavenbook</groupId>
                   <artifactId>simple-weather</artifactId>                  
                </javaModule>
                <webModule>
                   <groupId>org.sonatype.mavenbook</groupId>
                   <artifactId>simple-webapp</artifactId>          
                </webModule>                
               
             </modules>
           </configuration>
        </plugin>
     </plugins>
  </build>

</project>

Source Directory ändern

Das default source directory ist ${basedir}/src/main/java. So kann man das überschreiben:

<build>
<sourceDirectory>../src/java</sourceDirectory>

</build>

Http Proxy einrichten

In der Datei settings.xml foldenden Eintrag machen:

<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>myproxy</id>
     <active>true</active>
     <protocol>http</protocol>
     <host>proxy.somewhere</proxy>
     <port>8080</port>
     <username>blabla</username>
     <password>sdfs</password>
     <nonProxyHosts>*.blabla|*.hugo</nonProxyHosts>
   </proxy>
 </proxies>
</settings>

Die Datei befindet sich in .m2/settings/ oder $M2_HOME/conf/

Location vom lokalen Repository festlegen

In der Datei settings.xml foldenden Eintrag machen:

<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">

  <localRepository>${user.home}/mypath</localRepository>
</settings>

Die Datei settings.xml befindet sich in .m2/settings/ oder $M2_HOME/conf/

Artifakt in ein Remote Repository deployen

In url Element könnnen auch Pfade zum Filesystem angegeben werden.

<?xml version="1.0" encoding="UTF-8"?>
<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>org.sonatype.mavenbook</groupId>
  <artifactId>simple-parent</artifactId>
  <packaging>pom</packaging>
  <version>1.0</version>
  <name>Parent Project Maven2</name>
  <url>http://maven.apache.org</url>
  
  <modules>
    <module>simple-weather</module>
    <module>simple-webapp</module>
    <module>ear-app</module>
    <module>ejbmodule</module>
  </modules>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <distributionManagement>
    <repository>
      <id>mycompany-repository</id>
      <name>MyCompany Repository</name>
      <url>file:///home/claude/development/maven/dist</url>
    </repository>
  </distributionManagement>

</project>

Mit dem Befehl mvn deploy wird die ear Datei ins remote Repository kopiert.

Resourcen