Thursday, June 5, 2008

How to use Maven 2

How to use Maven 2

Maven is a tool. I will use maven to
  • create a blank project
  • make the project work in eclipse
  • add dependency for project
  • build
  • package
  • deploy

How to create a blank project?

create a normal java project
mvn archetype:create -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=example.tiger -DartifactId=JavaTiger

directory picture

create a project for web application
mvn archetype:create -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=example.maven.webapp -DartifactId=maven-webapp

directory picture

create a project for EJB application
The project for EJB is almost same with a normal java project. The only difference is that in EJB project there is one directory META-INF which includes ejb-jar.xml. The default directory in maven 2 is "src\main\resources\META-INF"
Hmm......But I have not found one archetypeArtifactId which is able to create this directory. "maven-archetype-j2ee-simple" is able to create it. But it will put ejb, web project in one project which I don't like.
Usually I will create a project by "maven-archetype-quickstart" and create resources\META-INF manually.
directory picture

Options for archetypeArtifactId
From the examples above, we can find out "archetypeArtifactId" is the key here. Then I have the question "what are the options for archetypeArtifactId?" I found some answers form here
Upon writing this documentation, there are ten (10) available archetype containers provided by maven These are:
- * maven-archetype-archetype
- * maven-archetype-j2ee-simple
- * maven-archetype-mojo
- * maven-archetype-portlet
- * maven-archetype-profiles (<work in progress>)
- * maven-archetype-quickstart
- * maven-archetype-simple (<work in progress>)
- * maven-archetype-site
- * maven-archetype-site-simple
- * maven-archetype-webapp

Question: Where can I find the descriptions or help documents for these archetypeArtifactId?

How to make the project created work in eclipse

Create project by eclipse
Usually I will create the project in the workspace. Then use eclipse to create the project with the same name. Eclipse will create .project and .classpath.

Import project into eclipse
If I create the project out of the workspace and import it into workspace, I will use maven to create .project and .classpath before it could be imported into a eclipse workspace.
mvn eclipse:eclipse

Add environment variable in workspace
Variable "M2_REPO" should be added into workspace so that eclipse can locate the maven 2 repository.
mvn eclipse:add-maven-repo -Declipse.workspace=Tiger

Tiger here is the workspace name.
Till now you can view your project in eclipse.

How to add dependency for project
using mvn2 eclipse plugin

How to exclude one jar file while generated WAR or EAR

Let’s explore each scope in detail:
compile is the default scope; all dependencies are compile-scoped if a scope is not supplied. compile dependencies are available in all classpaths, and they are packaged.
provided dependencies are used when you expect the JDK or a container to provide them. For example, if you were developing a web application, you would need the Servlet API available on the compile classpath to compile a servlet, but you wouldn’t want to include the Servlet API in the packaged WAR; the Servlet API JAR is supplied by your application server or servlet container. provided dependencies are available on the compilation classpath (not runtime). They are not transitive, nor are they packaged.
runtime dependencies are required to execute and test the system, but they are not required for compilation. For example, you may need a JDBC API JAR at compile time and the JDBC driver implementation only at runtime.
test-scoped dependencies are not required during the normal operation of an application, and they are available only during test compilation and execution phases. The test scope was previously introduced in Section 4.10, “Adding Test-scoped Dependencies”.”
The system scope is similar to provided except that you have to provide an explicit path to the JAR on the local file system. This is intended to allow compilation against native objects that may be part of the system libraries. The artifact is assumed to always be available and is not looked up in a repository. If you declare the scope to be system, you must also provide the systemPath element. Note that this scope is not recommended (you should always try to reference dependencies in a public or custom Maven repository).

How to copy a file

<copy file="target/targetPrj.war" todir="D:/jakarta-tomcat-5.5.9/webapps"></copy>

How do I skip the tests?

Add the parameter -Dmaven.test.skip=true in the command line

or you can ignore the result


Is it possible to create my own directory structure?
Absolutely yes!
By configuring <sourceDirectory>, <resources> and other elements of the <build> section.
In addition, you may need to change the plugin configuration if you are not using plugin defaults for their files/directories.

No comments:

Post a Comment