Choosing the scope of dependency

We may use a dependency for many reasons. Some of them may be required to compile and run the projects. There might be others only to run tests (for instance, junit). Then there may be dependencies that are required at runtime, say logback.

How to do it...

Use the following steps to choose the scope of the dependency:

  1. Open the Maven project we had created earlier.
  2. Observe the following section:
    <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
  3. Remove the preceding lines of code and run the following command:
    mvn compile
    
  4. Observe that it runs without any issues.
  5. Now, run the following command:
    mvn test
    
  6. Note the following error:
    [INFO] Compiling 1 source file to C:projectsapache-maven-cookbookproject-with
    -dependencies	arget	est-classes
    [INFO] -------------------------------------------------------------
    [ERROR] COMPILATION ERROR:
    [INFO] -------------------------------------------------------------
    [ERROR] /C:/projects/apache-maven-cookbook/project-with-dependencies/src/test/java/com/packt/cookbook/AppTest.java:[3,23] package junit.framework does not exist
    

How it works...

The Java source code App.java did not have any dependency. Only the source classes were compiled by mvn and thus, the command ran without any error.

The test code AppTest.java required the junit library to build. This is declared in the import statement in the code. The mvn test tried to compile the test classes, and as it did not find the dependency, it failed.

The following information needs to be specified to declare a dependency:

      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>

The preceding three elements uniquely identify the dependency.

The fourth piece of information is as follows:

<scope>test</scope>

By default, the scope is compile.

There are six different dependency scopes available:

  • compile: This dependency is required for compilation. This automatically means it is required for testing as well as runtime (when the project is run).
  • test: This dependency is only required for tests. This means the dependency is typically in the test code. As the test code is not used to run the project, these dependencies are not required for runtime.
  • runtime: These dependencies are not required during compilation, but only required to run the project. One example would be the logback dependency if you are using Simple Logging Facade for Java (slf4j) to log and want to use logback binding.
  • provided: This tells Maven that dependency is required for compilation and runtime, but this dependency need not be packaged with the package for distribution. The dependency will be provided by the user. An example of this dependency is servlet-api. Typically, application servers have these libraries.
  • system: This is similar to the provided scope. Here, we need to explicitly provide the location of the JAR file. It is not looked up from the repository. This may be useful to specify a dependency that is not present in the repository:
      <dependency>
          <groupId>com.myorg</groupId>
          <artifactId>some-jar</artifactId>
          <version>2.0</version>
          <scope>system</scope>
          <systemPath>${basedir}/lib/some.jar</systemPath>
        </dependency>
  • import: This is only used on a dependency of the pom type in the dependencyManagement section. It indicates that the specified pom should be replaced with the dependencies in that pom's dependencyManagement section. This is intended to centralize dependencies of large multi-module projects.

See also

  • The Manually installing dependencies that are not available in a repository recipe in this chapter
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.194.106