How to choose if a dependency will be included in your archive or not ?
When using Eclipse, if you Add a library to the Project build path is will be used just for compiling and it will not be included in your archive (e.g. in the WEB-INF/lib folder of your webapp)
The equivalent in Maven is using the scope provided in a dependency:

<dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-jpamodelgen</artifactId>
 <version>1.1.1.Final</version>
 <scope>provided</scope>
</dependency>

When using Eclipse, if you choose to Add classes to the Deployment Assembly, as shown by the following picture:

maven jboss tutorial

it means that the JAR file will be exported in your application. The equivalent in maven is using the compile scope.

<dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.15</version>
 <scope>compile</scope>
</dependency>


How to choose the name of your packaged application?
In the <build> section of your pom.xml , using the finalName element:


<build>
 <finalName>${project.artifactId}</finalName>
</build>

In this example, the name of the packaged archive is the project’s artifactId

 <artifactId>as7example</artifactId>

How to choose which JDK version will be used for compiling classes ?

maven jboss tutorial
You need to use the maven-compiler-plugin into the <build> section of your pom.xml

 <build>
 . . . .
 <plugins>
 <plugin>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>2.3.1</version>
 <configuration>
 <source>1.6</source>
 <target>1.6</target>
 </configuration>
 </plugin>
 . . . .
</build>

In this example, the compiler plugin enforces Java 1.6 compatibility.


How to add resources to a JAR ?
When using Eclipse, if you want to add a resource into an archive then you could simply place it into the “src” or “ejbProject” of your project, and it will be packaged together with the application classes, at the same level you have added it into your source folder.

Maven does pretty much the same, just it uses the resources directory structure to store files or resources.
The simple rule employed by Maven is this: any directories or files placed within the ${basedir}/src/main/resources directory are packaged in your JAR with the exact same structure starting at the base of the JAR.

my-app
|-- pom.xml
`-- src
|-- main
| |-- java
| | `-- com
| | `-- mycompany
| | `-- app
| | `-- App.java
| `-- resources
| `-- META-INF
| `-- application.properties
`-- test
`-- java
`-- com
`-- mycompany
`-- app
`-- AppTest.java

 

Therefore, if you package this application, the application.properties file will be stored under the META-INF root folder of your archive.
How to run a JUnit test with Maven

maven jboss tutorial

Well at first you need to include the JUnit dependency in your pom.xml:

<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.8.2</version>
 <scope>test</scope>
</dependency>


Next create your test cases reside in the <project_base_dir>/src/test/java folder.
Executing the test build phase from the command line executes the tests and all other phases required for the test phase:


$ mvn test

Alternatively, the test phase is automatically executed in the default build lifecycle. Therefore, executing install, for example, will run all the phases including the test phase:

$ mvn install
Apache Maven outputs the test results on the console. You should see something similar to:
-------------------------------------------------------T E S T S
-------------------------------------------------------Running com.sample.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.023 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

0
0
0
s2sdefault

Related articles available on mastertheboss.com

JBoss Maven example: building a Java EE 6 application

In this tutorial we will show how to create a basic Java EE 6 app

Eclipse and Maven tutorial

In this tutorial we will show how you can create a Maven project

Maven and JBoss a concrete example: Infinispan

If you have completed the basic Maven and Eclipse tutorial we wil

Maven JBoss AS 7 plugin tutorial

The Maven JBoss plugin can be used to perform some common tasks l

Maven and JBoss: how to use BOMs to keep releases in sync

When using Maven for creating and building your projects you have

Maven Multi module tutorial

In this tutorial we will show how to use Maven multi-module proje