Maven

From Null-pointer

Jump to: navigation, search

Contents

Command line params

Work offline

-o

[root@localhost ~]# mvn -o clean install

skip tests

-DskipTests

[root@localhost ~]# mvn install -DskipTests

dependency tree

[root@localhost ~]# mvn dependency:tree

Dependency scope

Maven provides four dependency scopes [2]:

  • compile: A compile-scope dependency is available in all phases. This is the default value.
  • provided: A provided dependency is used to compile the application, but will not be deployed. You would use this scope when you expect the JDK or application server to provide the JAR. The servlet APIs are a good example.
  • runtime: Runtime-scope dependencies are not needed for compilation, only for execution, such as JDBC (Java Database Connectivity) drivers.
  • test: Test-scope dependencies are needed only to compile and run tests (JUnit, for example).

Executable jar

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-5</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>package.to.MainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <!-- this is used for inheritance merges -->
                        <id>make-assembly</id>
                        <!-- append to the packaging phase. -->
                        <phase>package</phase>
                        <goals>
                            <!-- goals == mojos -->
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
 
            </plugin>
        </plugins>
    </build>

Generating DDL Scripts from JPA Annotations

pom.xml

  1. <build>
  2.   <plugins>
  3.     <plugin>
  4.       <groupId>org.codehaus.mojo</groupId>
  5.       <artifactId>hibernate3-maven-plugin</artifactId>
  6.       <version>2.2</version>
  7.       <executions>
  8.         <execution>
  9.           <phase>process-classes</phase>
  10.           <goals>
  11.             <goal>hbm2ddl</goal>
  12.           </goals>
  13.         </execution>
  14.       </executions>
  15.       <configuration>
  16.         <components>
  17.           <component>
  18.             <name>hbm2ddl</name>
  19.             <implementation>jpaconfiguration</implementation>
  20.           </component>
  21.         </components>
  22.         <componentProperties>
  23.           <persistenceunit>arbiter</persistenceunit>
  24.           <outputfilename>schema.ddl</outputfilename>
  25.           <drop>false</drop>
  26.           <create>true</create>
  27.           <export>false</export>
  28.           <format>true</format>
  29.         </componentProperties>
  30.       </configuration>
  31.     </plugin>
  32.    </plugins>
  33.   </build>

persistence.xml

  • in src/main/resources/META-INF

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="arbiter"> <jta-data-source>java:/DefaultDS</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/> </properties> </persistence-unit> </persistence>

command

[root@localhost ~]# mvn hibernate3:hbm2ddl

sources

Overlays

you need the type of war and scope of runtime

<dependency>
	<groupId>bbc.iplayer.jet</groupId>
	<artifactId>jet-common-dynamic-logging</artifactId>
	<version>10.16.1</version>
	<type>war</type>
	<scope>runtime</scope>
</dependency>

exclusions

<build>
	<finalName>ace</finalName>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-war-plugin</artifactId>
			<configuration>
			<overlays>
				<overlay>
				<groupId>bbc.iplayer.jet</groupId>
				<artifactId>jet-common-dynamic-logging</artifactId>
				<excludes>
				  <exclude>WEB-INF/web.xml</exclude>
				  <exclude>WEB-INF/lib/**</exclude>
				</excludes>
				</overlay>
			</overlays>
			</configuration>
		</plugin>
	</plugins>
 
</build>

maven lifecycle

Java 1.6

N.B. not that the source tag is not closed. This is just a hack to get it to display on this wiki.

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.6</source
                    <target>1.6</target>
                    <optimize>true</optimize>
                </configuration>
            </plugin>
        </plugins>
    </build>

jetty

<build>
        <finalName>myWar</finalName>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.23</version>
                <configuration>
                    <contextPath>/</contextPath>
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>8085</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
 
            </plugin>
 
        </plugins>
    </build>

[root@localhost ~]# mvn jetty:run


debug

[root@localhost ~]# export MAVEN_OPTS='-Xmx1024m -Xms512m -XX:MaxPermSize=256m -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005' [3]

References

  1. http://maven.apache.org/plugins/maven-surefire-plugin/examples/skipping-test.html
  2. http://www.javaworld.com/javaworld/jw-12-2005/jw-1205-maven.html?page=2
  3. "Maven Jetty plugin" Retrieved 12 May 2011.
Personal tools