Advertise with us

Search Mastertheboss

Our eBooks

Maven for Eclipse Users Tutorials - Maven for Eclipse users tutorial part 2

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:


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.


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


In this example, the name of the packaged archive is the project’s 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

 . . . .
 . . . .

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.

|-- pom.xml
`-- src
|-- main
| |-- java
| | `-- com
| | `-- mycompany
| | `-- app
| | `--
| `-- resources
| `-- META-INF
| `--
`-- test
`-- java
`-- com
`-- mycompany
`-- app


Therefore, if you package this application, the 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:


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

© 2021 All Rights Reserved.

Please publish modules in offcanvas position.