Tech notes for maven


Maven p19: Starting a Java webapp

You can create a simple java webapp with this, noting the archetypeArtifactId.

	mvn archetype:create -DgroupId=your.namespace -DartifactId=yourwebapp -DarchetypeArtifactId=maven-archetype-webapp

This is create very simple project, with the web descriptor in src/main/webapp/WEB-INF/web.xml. This is where you configure the app, but at the moment there's on the display-name in, which if ever used is only used in IDEs and such descriptively. The src/main/webapp/index.jsp is just some simple HTML, bigger apps it would have java commands delimited with <% %>.

To run this quickly, instead of setting up Tomcat or whatever, you can install the jetty plugin for maven, allowing you to start a small web server to see the webapp quickly. Put this in your pom.xml within the build tag:

	<plugins>
		<plugin>
			<groupId>org.mortbay.jetty</groupId>
			<artifactId>maven-jetty-plugin</artifactId>
			<version>6.1.26</version>
		</plugin>
	</plugins>

Now you can run mvn jetty:run and your webapp will be available at http://127.0.0.1:8080/yourwebapp/

Obviously this has not servlets or anything, so it's very basic!

maven java-jetty java

Maven p18: Exec plugin

The exec plugin can help you run a java project without using the java-with-dependencies assembly. You run it from the command line.

Issue this:

	mvn exec:java -Dexec.mainClass=your.package.YourMain
maven

Maven p16: Assembly, java-with-dependencies

The standard jar created will not include any dependencies included, as a quick way around this you can create a jar with dependencies. This is all the byte code included in the same jar, ergo it will be large.

In the build then plugins tag:

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

We're using the assembly plugin, and configuring it to create a description jar-with-dependencies.

This won't be created automatically. We'll have to run mvn install assembly:assembly to create a jar in target/itsname-jar-with-dependencies.jar

To ensure it is created automatically, change its configuration to tell it to run on execution by specifying a executions -> execution, with the phase and the configuration within that.

	<plugin>
		<artifactId>maven-assembly-plugin</artifactId>
		<executions>
			<execution>
				<id>itsname</id>
				<phase>package</phase>
				<goals>
					<goal>single</goal>
				</goals>
				<configuration>
					<descriptorRefs>
						<descriptorRef>
							jar-with-dependencies
						</descriptorRef>
					</descriptorRefs>
				</configuration>
			</execution>
		</executions>
	</plugin>
maven

Maven p17: Resources

You can add resources in both main and test in your project. Add a text file in src/main/resources. Then you can access that via

	Reader ios = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("file.txt"));		
	String s = IOUtils.toString(ios);
	System.out.println(s);

This is because the file.txt will be in target/classes/ now

In your test directory, you can put different resources there. A file in src/test/resources/ can be accessed via similar methods, but this time in the test java files.

You can also filter these resource files. That is, define a filter, which is a key value type file, which will be used to replace values in the resource file. Here's the default.properties in src/main/filters

	a.property=brilliant

Here's a test.xml in src/main/resources

	<some>${a.property}</some>

Now in our POM we need to define that the xml will be filtered

	...
	<build>
	<filters>
	  <filter>src/main/filters/default.properties</filter>
	</filters>    
	<resources>
	  <resource>
	    <directory>src/main/resources/</directory>
	    <filtering>true</filtering>
	  </resource>
	</resources>
	</build>
	...

If you mvn package this application the process-resources phase will be run, and you'll get this XML file in your target/classes/test.xml

	<some>brilliant</some>
maven

Maven p15: Site generation

In 3.0, you can get maven to produce various reports for you, from SCM, to mailing lists to unit tests. The report plugin has itself plugins to perform all kinds of reports.

First add a plugin within the build tag of your project. We'll deal with the stuff in configuration after.

	<build>
	<plugins>
	    <plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-site-plugin</artifactId>
		<version>3.0-beta-3</version>
				<configuration>
				...
				</configuration>
			</plugin>
			...
		</plugins>
		...
	</build>

So we're adding a plugin as usual. Now for the configuration part.

The first thing in configuration is reportPlugins, which we need to actually do anything.

    <configuration>
		<reportPlugins>
		<plugin>
		...
		</plugin>
		...
	</reportPlugins>
    </configuration>

So let's look at the plugins we can define.

   <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-project-info-reports-plugin</artifactId>
      <version>2.2</version>
      <reports>
	<report>cim</report>
	<report>issue-tracking</report>
	<report>dependencies</report>
	<report>index</report>
	<report>help</report>
	<report>scm</report>
	<report>summary</report>
	<report>mailing-list</report>
      </reports>
    </plugin>

This specifies reports for issue-tracking, scm etc. Obviously you need to configure these, so they'll be blank at the moment.

The next two configure javadoc and unit test reports.

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-javadoc-plugin</artifactId>
      <version>2.7</version>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-report-plugin</artifactId>
      <version>2.12</version>
    </plugin>
maven

Page 2 of 5
prev next
Click me