Maven – Dependency Scope

image

with below command you can see all the dependency in the command prompt

projectpath$ mvn dependency:tree

Project Dependencies briefly introduced three of the five dependency scopes: compile, test, and provided. Scope controls which dependencies are available in which classpath, and which dependencies are included with an application. Let’s explore each scope in detail:

compile

compile+ is the default scope; all dependencies are compile-scoped if a scope is not supplied. compiledependencies are available in all classpaths, and they are packaged.

provided

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

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

test+-scoped dependencies are not required during the normal operation of an application, and they are available only during test compilation and execution phases.

system

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 besystem, 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).
Advertisements

Maven 2 lifecycle phases

In Maven 2, this notion is standardized into a set of well-known and well-defined lifecycle phases(see figure). Instead of invoking plug-ins, the Maven 2 developer invokes a lifecycle phase: $mvn compile. All the dependencies, goals, tasks, plugins which belong to this life cycle phase will be invoked otherwise not.

maven life cycle phases

maven life cycle phases

For example if Trinidad report plugin is required to be executed during test phase then it can be configured in below way

 <execution>
    <id>Trinidad-report</id>
    <phase>compile</phase>
    <goals>
        <goal>java</goal>
    </goals>
    <configuration>
        <mainClass>com.ptc.fusion.fitnesse.fixture.SetDatesAsSystemProperties</mainClass>
    </configuration>
</execution>

 

Some useful Maven 2 lifecycle phases are the following:

  • generate-sources: Generates any extra source code needed for the application, which is generally accomplished using the appropriate plug-ins
  • compile: Compiles the project source code
  • test-compile: Compiles the project unit tests
  • test: Runs the unit tests (typically using JUnit) in the src/test directory
  • package: Packages the compiled code in its distributable format (JAR, WAR, etc.)
  • integration-test: Processes and deploys the package if necessary into an environment where integration tests can be run
  • install: Installs the package into the local repository for use as a dependency in other projects on your local machine
  • deploy: Done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects

 

The lifecycle phase invokes the plug-ins it needs to do the job. Invoking a lifecycle phase automatically invokes any previous lifecycle phases as well. Since the lifecycle phases are limited in number, easy to understand, and well organized, becoming familiar with the lifecycle of a new Maven 2 project is easy.

 

All Maven Default Lifecycle Phases

  1. validate
  2. generate-sources
  3. process-sources
  4. generate-resources
  5. process-resources
  6. compile
  7. process-classes
  8. generate-test-sources
  9. process-test-sources
  10. generate-test-resources
  11. process-test-resources
  12. test-compile
  13. test
  14. prepare-package (maven 2.1+)
  15. package
  16. pre-integration-test
  17. integration-test
  18. post-integration-test
  19. verify
  20. install
  21. deploy

There are a couple key concepts to be aware of with maven lifecycles. First off, if we call a particular phase via a maven command, such as “mvn compile”, all phases up to and including that phase will be executed. So, in the case of “mvn compile”, we would actually go through the validate, generate-sources, process-sources, generate-resources, process-resources, and compile phases. The second main concept to be aware of in regards to lifecycles is that, based on the packaging of a project (jar, war, ear, etc), different maven goals will be bound to different phases of the maven lifecycle.