Maven

swatinikam2009 created the topic: Maven
Hi ,

I am trying to run java code. I have set the environment correctly. Also created MAVEN_HOME variable and added it to path varibale.
But still i am getting error message saying “count not find or load main class App”.

What to do?

Tagged :

Maven

swatinikam2009 created the topic: Maven
Hi ,

I am trying to run java code. I have set the environment correctly. Also created MAVEN_HOME variable and added it to path varibale.
But still i am getting error message saying “count not find or load main class App”.

What to do?

rajeshkumar replied the topic: Maven
Check with your java file main method or maven directory structure.

Example
github.com/scmgalaxy/jacoco-maven-unittestv2/
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

swatinikam2009 replied the topic: Maven
Hi Rajesh,

I have done all the settings proprly but still getting the error.

So I tried to run code by followinf cmd.

c:\MavenDemos\MyPro > java -cp target/classes com.acc.App

And then it is showing me output.

Can you please tell me. Where i am doing wrong?

swatinikam2009 replied the topic: Maven
Contiued with the above query.

I have done one change. And then it is working fine . No need to use c:\MavenDemos\MyPro>java -cp target/classes com.acc.App

I have removed CLASSPATH variable from user variable in Env. variables.

Then it is working fine. so now i just type c:\MavenDemos\MyPro\target\classes>java com.acc.App

Tagged :

Maven2 Step by Step Guide – Complete Introduction

maven2-step-by-step

Setting up the application frame

The Maven2 documentation is silent about how to set up web application skeletons. The standard archetypes allow you to create a simple standalone, a simple web application (no Java code), and a multi-module project. Presumably, the recommended approach to creating a standard web application (JSP and HTML code for the view, and Java code for the Model and Controller layers) is to create a multi-module project containing a single simple web application and one or more standalone applications for the Model and Controller. However, it is possible to make Maven2 work with a standard web application structure by adding some directories to the skeleton created by the standalone Maven archetype.

The Maven2 Getting started Guide has some commands for creating skeletons from standard Maven archetypes. To create the skeleton for an application my-app in the com.mycompany.app namespace using a standalone archetype, run this command:

1
$ mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app

To make a standard web application skeleton from this, add the following directories. The webapps directories is the root of the web applications context, and contains the WEB-INF directory. The resources subdirectory corresponds to the WEB-INF/classes directory for a web application or the conf/ or etc/ directory for a standalone, where all the properties files go. The test/resources/ directory is useful for storing test specific properties files.

1
2
3
4
my-app/src/main/webapps/
my-app/src/main/webapps/WEB-INF/
my-app/src/main/resources/
my-app/src/test/resources/

Finally, to make a web application out of the standard standalone application, change the value of project.packaging from jar to war in the pom.xml file.

Standard Maven2 Goals

Maven2 provides some standard goals which are similar to Ant targets found in most project’s build.xml files. This helps people familiar with Ant to make a quick transition to using Maven2. They are as follows:

1
2
3
4
5
6
mvn clean - cleans out all Maven2 generated files.
mvn compile - compiles the Java sources
mvn test-compile - compiles the Java JUnit test classes.
mvn test - runs all JUnit tests in the project.
mvn package - builds the JAR or WAR file for the project.
mvn install - installs the JAR or WAR file in the local Maven repository.

Most of these goals are configured in the super-POM, but if you want to build your application using Java 1.5 (who doesn’t these days), you will need some additional configuration in your pom.xml, in the project.build.plugins element in your pom.xml.

1
2
3
4
5
6
7
8
9
      <!-- Java 1.5 compiler plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>

Generating IDE configuration files

Most people work with some kind of IDE, and the IDE must be told where to look for JAR files, Java code, test code, etc. Since this information is already specified in the pom.xml files, Maven2 can generate the IDE artefacts for a large number of popular IDEs in use, such as Eclipse, IDEA and Emacs JDEE. To generate the Eclipse artefacts for your project, run the following command:

1
$ mvn eclipse:eclipse

You also need to tell Eclipse where to find the your local Maven2 repository (under ~/.m2/repository by default). You can do this by setting a global environment variable M2_REPO in the IDE.

Running Jetty in place (webapps)

A nice touch is the Maven2 Jetty Plugin. This starts up a Jetty installation in place in your web application. This is a huge time-saver, since web application development is often an iterative cycle of changing code, deploying to application server, checking a web page and back. Having Jetty running on your web application source enables you to see your changes instantly (or after compiling with changes in Java code). To set it up, you will need to add the following configuration in project.build.plugins in your pom.xml file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty6-plugin</artifactId>
        <configuration>
          <scanIntervalSeconds>10</scanIntervalSeconds>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-j2ee_1.4_spec</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
          </dependency>
        </dependencies>
      </plugin>

Then start up Jetty with the following command. You should be able to see your web application on your browser at http://localhost:8080/my-app.

1
$ mvn jetty6:run

Documentation: The project website

I think of documentation as a place to tell the world all about the cool things my project does. However, too many projects skimp on documentation, because of the effort in setting up the infrastructure in place to generate documentation. Maven2 has built in support for generating a project website with all the documentation for your project. Data entered into the pom.xml drives a lot of the reports, such as Mailing List, Issue Management, Source Repository, etc. Maven2 needs to be told which reports to generate in the project.reporting.plugins section of the pom.xml. Details about which pom.xml entries drive which report elements can be found in Resources[2].

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <reportSets>
          <reportSet>
            <reports>
              <report>dependencies</report>
              <report>project-team</report>
              <report>mailing-list</report>
              <report>cim</report>
              <report>issue-tracking</report>
              <!-- <report>license</report> -->
              <report>scm</report>
            </reports>
          </reportSet>
        </reportSets>
      </plugin>

You can also create project documentation content, such as User Guides, etc, in a variety of formats, such as DocBook and APT (Almost Plain Text). APT is a wiki-like plain text format which I find very convenient. For FAQ entries, Maven2 supports the FML format.

To set up the project site, create the following directories.

1
2
3
4
5
my-app/src/site/apt/
my-app/src/site/fml/
my-app/src/site/resources/
my-app/src/site/resources/images/
my-app/src/site/site.xml

The apt subdirectory should contain the documentation in APT format. The fml subdirectory contains various FAQ source files. The resources subdirectory should contain files and documents that do not need any pre-processing. The files in the apt and fml subdirectory will be pre-processed by the APT and FML processor and be copied to the project site root directory as .html files. The files in the resources subdirectory (including the images/ subdirectory) will be copied to the project site docroot as is. The site.xml specifies the template for your site, ie what the left and right banners should contain, etc. The site.xml file will reference these HTML files.

To generate the project web site, run the following command. The website will be generated under my-app/target/site where you can view it with a browser using the file:// protocol.

1
$ mvn site

Javadocs

The Javadocs plugin needs to be configured explicitly in the project pom.xml in the project.reporting.plugins section.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <configuration>
          <links>
            <link>http://java.sun.com/j2se/1.5.0/docs/api/</link>
            <link>http://plexus.codehaus.org/ref/1.0-alpha-9/apidocs</link>
            <link>http://jakarta.apache.org/commons/dbcp/apidocs/</link>
            ... other third party libs used by your project ...
          </links>
        </configuration>
      </plugin>

If the Javadocs plugin has been configured, then mvn site will automatically generate the Javadocs. Otherwise, they can be generated separately using the command:

1
$ mvn javadoc:javadoc

Java Cross Reference (JXR)

Another cool plugin is the Code Cross Reference that is created using the JXR plugin. The project code is made displayable with line numbers and syntax highlighting, with links to related code. Almost like having an IDE running on the browser in read-only mode. Though not required for all projects, this is very useful to allow people to review your code without having to download it. To configure it, configure the plugin in the project.reporting.plugins section.

1
2
3
4
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jxr-plugin</artifactId>
      </plugin>

Like the Javadocs goal, this will be run as part of mvn site as well, but can be run separately using the command:

1
mvn jxr:jxr

Code Style (Checkstyle)

I chose Checkstyle because I am familiar with it, but I think Maven2 supports other code style checkers too. I use the default Maven code style, since this is likely to become the most popular code style as more and more people embrace Maven2, but this setting can be overriden to use Sun’s code style or some other style preferred by your corporation. To configure it (with default Maven code style), we need the following configuration in project.reporting.plugins in the pom.xml file.

1
2
3
4
5
6
7
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <configuration>
          <configLocation>config/maven_checks.xml</configLocation>
        </configuration>
      </plugin>

As before, once configured, the report is run as part of the mvn site command, but can be run separately using the command:

1
$ mvn checkstyle:checkstyle

Unit Test Coverage (Cobertura)

Clover is the most popular tool for checking on Unit test coverage, and Maven2 provides a plugin for that. However, Clover is commercial software and costs money. A free, open-source alternative is Cobertura. Maven2 provides a Cobertura plugin as well. To configure this plugin, add the following XML to project.reporting.plugins section in the pom.xml.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <!--
        <executions>
          <execution>
            <id>clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
        -->
      </plugin>

The plugin can be run manually using the command:

1
$ mvn cobertura:cobertura

The plugin leaves a cobertura.ser file in the project root directory, which can be removed using mvn clean. There was a suggestion to automate this by creating an executions subelement under the plugin declaration, but I could not get it to work – the parser complained about invalid tags, so I commented it out (in the above XML snippet). If anyone knows what I did wrong, please let me know.

Deploying files to other locations

I dont know much about this, except that it involves using the Maven2 Wagon Plugin. The plugin supports a wide variety of transport formats. I will post more information about this as I find it. If anyone has some information about using Wagon, would appreciate your posting it here or pointing to links with the information.

So thats basically all I have for now. Hopefully, this article will help kickstart Maven2 adoption in your new projects. Personally, I have used Ant for over 6 years now, and I was justifiably sceptical about switching new development to Maven2. However, I have had good success with Maven2 so far, and I am quite impressed with its range of capabilities in terms of integration with other tools and the services it provides to the developer.

Resources

  • The Maven 2 POM demystified – I have restructured my template pom.xml according to this article, and it helps a lot in understanding the POM.
  • Get the most out of Maven2 site generation – Much useful information about what elements to populate to produce clear Maven2 project reports.
  • The Maven2 Schema – Its hard to figure out what element goes where without examples. The XSD is very detailed and provides good information.
  • Better Builds with Maven – if you haven’t already downloaded this free e-book, you should. This is the only book I know of which talks about Maven2, so if you are working with Maven2, you should definitely download and read it.
  • Maven : A Developer’s Notebook – covers details of the FML format used for generating FAQ entries.
Tagged : / / / / / / / / / /

Reporting Plugins in Maven | Maven Plugins That Provide Reports

reporting-plugins-in-maven

Reporting plugins
Plugins which generate reports, are configured as reports in the POM and run under the site generation
lifecycle.

This plugin consists of several reports that you can run selectively, individually or even run all of them. Please see below for instructions on how to configure your pom.xml to do this.

Run All Reports
To include all Project Info Reports in your project, you must configure your pom.xml. Use “mvn site:site” to generate the configured reports.

project


org.apache.maven.plugins
maven-project-info-reports-plugin

Run Selective Reports


org.apache.maven.plugins
maven-project-info-reports-plugin

dependencies
project-team
mailing-list
cim
issue-tracking
license
scm

Reporting plugins are configured in the POM as in this example:

org.apache.maven.plugins
maven-project-info-reports-plugin

Configuration in the reporting section also applies if plugin goals are invoked individually. The converse is not true– if you configure the plugin inside , that configuration will NOT apply to .

Configuring multiple reports for one plugin using . Each must have a unique . For more information on UMLGraph, see the UMLGraph site and this page.

org.apache.maven.plugins
maven-javadoc-plugin

uml

gr.spinellis.umlgraph.doclet.UmlGraph

gr.spinellis
UmlGraph
4.4

-views
target/uml
private

javadoc

html

private

javadoc

Plugins Type Version Release Date Description Source Repository Issue Tracking
changelog R 2.1 2007-07-25 Generate a
list of recent
changes from
your SCM.
SVN JIRA
changes B+R 2.1 2008-11-24 Generate a
report from
issue tracking
or a change
document.
SVN JIRA
checkstyle B+R 2.3 2009-07-14 Generate a
checkstyle
report.
SVN JIRA
clover B+R 2.4 2007-04-23 Generate
a Clover
report. NOTE:
Moved to
Atlassian.com
SVN JIRA
doap B 1.0 2008-08-01 Generate a
Description
of a Project
(DOAP) file
from a POM.
SVN JIRA
docck B 1.0 2008-11-16 Documentation
checker
plugin.
SVN JIRA
javadoc B+R 2.6 2009-07-29 Generate
Javadoc for
the project.
SVN JIRA
Jxr R 2.1 2007-04-05 Generate a
source cross
reference.
SVN JIRA
pmd B+R 2.4 2008-01-08 Generate a
PMD report.
SVN JIRA
projectinforeports R 2.1.2 2009-07-23 Generate
standard
project
reports.
SVN JIRA
surefirereport R 2.4.3 2008-05-14 Generate a
report based
on the results
of unit tests.
SVN JIRA

Known Plugins That Provide Reports

 

Tagged : / / / / / / / / / / / /

Properties in Maven – List of Maven Properties

properties-in-maven

Intro
It is a collection of things found in the offcial maven documentation and postings to the maven user mailing list.
Build in properties

  • ${basedir} represents the directory containing pom.xml
  • ${version} equivalent to ${project.version} or ${pom.version}

Pom/Project properties
All elements in the pom.xml, can be referenced with the project. prefix or using pom. as prefix. This list is just an example of some commonly used elements.

  • ${project.build.directory} results in the path to your “target” dir, this is the same as ${pom.project.build.directory}
  • ${project.build.outputDirectory} results in the path to your “target/classes” dir
  • ${project.name} or ${pom.name} refers to the name of the project.
  • ${project.version} or ${pom.version} refers to the version of the project.
  • ${project.build.finalName} refers to the final name of the file created when the built project is packaged

Local user settings
Similarly, values in the user’s settings.xml can be referenced using property names with settings. prefix.

  • ${settings.localRepository} refers to the path of the user’s local repository.
  • ${maven.repo.local} also works for backward compatibility with maven1 ??

Environment variables
Environment variables can be referenced using the env prefix

  • ${env.M2_HOME} returns the Maven2 installation path.
  • ${java.home} specifies the path to the current JRE_HOME environment use with relative paths to get for example:
    ${java.home}../bin/java.exe

Java system properties
All Java System Properties defined by the JVM.
Custom properties in the POM
User defined properties in the pom.xml.

hello

  • ${my.filter.value} will result in hello if you inserted the above XML fragment in your pom.xml

Parent Project variables
How can parent project variables be accessed?
You can use the prefix: ${project.parent}.
A good way to determine possible variables is to have a look directly at the API. I’m currently using Maven 2.2.1, and to access the Parent you can use ${project.parent}. This will return an org.apache.maven.project.MavenProject instance.
To access the parent version: ${parent.version}.
Reflection Properties
The pattern ${someX.someY.someZ} can simply sometimes mean getSomeX().getSomeY().getSomeZ(). Thus, properties such as ${project.build.directory} is translated to getProject().getBuild().getDirectory().
List of Maven Properties

project (from [1])
o project.distributionManagementArtifactRepository
o project.artifact
o project.parent
o project.file
o project.artifacts
o project.parentArtifact
o project.pluginArtifacts
o project.remoteArtifactRepositories
o project.pluginArtifactRepositories
o project.attachedArtifact
* settings (from [2])
o settings.offilne
o settings.interactive
* rootless (from [3])
o localRepository
o reactorProjects
* java properties (from [4])
o java.version
o java.vendor
o java.vendor.url
o java.home
o java.vm.specification.version
o java.vm.specification.vendor
o java.vm.specification.name
o java.vm.version
o java.vm.vendor
o java.vm.name
o java.specification.version
o java.specification.vendor
o java.specification.name
o java.class.version
o java.class.path
o java.library.path
o java.io.tmpdir
o java.compiler
o java.ext.dirs
o os.name
o os.arch
o os.version
o file.separator
o path.separator
o line.separator
o user.name
o user.home
o user.dir

 

Tagged : / / / / / / / / / / / / /

Profiles in Maven – How to Build Maven Profile ?

profiles-in-maven

Reference: Apache Maven,Current version User Guide

Profiles in Maven

 Use of profile:

Maven 2.0 goes to great lengths to ensure that builds are portable. Among other things, this means allowing build configuration inside the POM, avoiding all filesystem references (in inhertiance,
dependencies, and other places), and leaning much more heavily on the local repository to store the metadata needed to make this possible.

However, sometimes portability is not entirely possible. Under certain conditions, plugins may need to be configured with local filesystem paths. Under other circumstances, a slightly different
dependency set will be required, and the project’s artifact name may need to be adjusted slightly. And at still other times, you may even need to include a whole plugin in the build lifecycle depending on the detected build environment.

To address these circumstances, Maven 2.0 introduces the concept of a build profile. Profiles are specified using a subset of the elements available in the POM itself (plus one extra section), and are
triggered in any of a variety of ways. They modify the POM at build time, and are meant to be used in complementary sets to give equivalent-but-different parameters for a set of target environments
(providing, for example, the path of the appserver root in the development, testing, and production environments). As such, profiles can easily lead to differing build results from different members of your team. However, used properly, profiles can be used while still preserving project portability. This will also minimize the use of -f option of maven which allows user to create another POM with different parameters or configuration to build which makes it more maintainable since it is running with one POM only.

What are the different types of profile? Where is each defined?

• Per Project: Defined in the POM itself (pom.xml).
• Per User: Defined in the Maven-settings (%USER_HOME%/.m2/settings.xml).
• Global: Defined in the global maven-settings (%M2_HOME%/conf/settings.xml).
• Profile descriptor: a descriptor located in project basedir (profiles.xml)

How can a profile be triggered? How does this vary according to the type of profile being used?

  • A profile can be triggered/activated in several ways:
  • Explicitly
  • Through Maven settings
  • Based on environment variables
  • OS settings
  • Present or missing files

Profiles can be explicitly specified using the -P CLI option.

This option takes an argument that is a comma-delimited list of profile-ids to use. When this option is specified, no profiles other than those specified in the option argument will be activated.

mvn groupId:artifactId:goal -P profile-1,profile-2

Profiles can be activated in the Maven settings, via the section.

This section takes a list of elements, each containing a profile-id inside.

profile-1

Profiles listed in the tag would be activated by default everytime a project use it.

Profiles can be automatically triggered based on the detected state of the build environment.

These triggers are specified via an section in the profile itself. Currently, this detection is
limited to prefix-matching of the JDK version, the presence of a system property or the value of a
system property. Here are some examples.
The follwing configuration will trigger the profile when the JDK’s version starts with “1.4” (eg.
“1.4.0_08”, “1.4.2_07”, “1.4”):

1.4


The following honours versions 1.3, 1.4 and 1.5.

[1.3,1.6)


Note: an upper bound such as ,1.5] is likely not to include most releases of 1.5, since they will have an additional “patch” release such as _05 that is not taken into consideration in the above range.

This next one will activate based on OS settings.

See the Maven Enforcer Plugin for more details about OS values.

Windows XP
Windows
x86
5.1.2600


This will activate the profile when the system property “debug” is specified with any value:

debug


This example will trigger the profile when the system property “environment” is specified with the
value “test”:

environment
test


Note: Environment variable FOO would be set like env.FOO.

Present or missing files

To activate this you would type this on the command line: mvn groupId:artifactId:goal -Denvironment=test

This example will trigger the profile when the generated file target/generated-sources/axistools/wsdl2java/org/apache/maven is missing.

target/generated-sources/axistools/wsdl2java/org/apache/maven


Note: The tags and could be interpolated with some patterns like ${user.home}. Profiles can also be active by default using a configuration like the following:

profile-1

true


This profile will automatically be active for all builds unless another profile in the same pom is activated using one of the previously described methods. All profiles that are active by default are automatically deactivated when a profile in the pom is activated on the command line or through its activation config. 20.1.2.2

Deactivating a profile

Starting with Maven 2.0.10, one or more profiles can be deactivated using the command line by prefixing their identifier with either the character ‘!’ or ‘-‘ as shown below:

mvn groupId:artifactId:goal -P !profile-1,!profile-2

This can be used to deactivate profiles marked as activeByDefault or profiles that would otherwise be activated through their activation config.

Next ….? Coming Soon

Tagged : / / / / / / / / / / / /