Monday, 4 December 2017

Maven - Introduction to the dependency management

The "Transitive dependencies" feature allows you to avoid needing to discover and specify the libraries that your own dependencies require, and including them automatically.
The "dependency scope" is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks. There are 6 scopes available:
  • "compile" is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project.
  • "provided" is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime.
  • "runtime" indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
  • "test" indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.
  • "system" is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
  • "import" is only supported on a dependency of type pom in the <dependencyManagement> section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.
The "Dependency Management" section is a mechanism for centralizing dependency information. It can be used when two POMs extend the same parent. Example:
Pom of Project A
   <project>
      ...
      <dependencies>
        <dependency>
          <groupId>it.blogspot.informaticaamodomio</groupId>
          <artifactId>my-app</artifactId>
          <version>1.0</version>
          <exclusions>
            <exclusion>
              <groupId>it.blogspot.informaticaamodomio</groupId>
              <artifactId>my-artifact</artifactId>
            </exclusion>
          </exclusions>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <type>jar</type>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>
Pom of Project B
   <project>
      ...
      <dependencies>
        <dependency>
          <groupId>it.blogspot.informaticaamodomio</groupId>
          <artifactId>my-artifact-b</artifactId>
          <version>1.0</version>
          <type>war</type>
          <scope>runtime</scope>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <type>jar</type>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>
Now we can apply the dependency management adding the dependencyManagement section in Pom Parent (the order of dependencies is not relevant):
   <project>
      ...
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>it.blogspot.informaticaamodomio</groupId>
            <artifactId>my-artifact-b</artifactId>
            <version>1.0</version>
            <type>war</type>
            <scope>runtime</scope>
          </dependency>
          <dependency>
            <groupId>it.blogspot.informaticaamodomio</groupId>
            <artifactId>my-app</artifactId>
            <version>1.0</version>
            <exclusions>
              <exclusion>
                <groupId>it.blogspot.informaticaamodomio</groupId>
                <artifactId>my-artifact</artifactId>
              </exclusion>
            </exclusions>
          </dependency>
          <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <type>jar</type>
            <scope>test</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
    </project>
Now update the dependencies section in both Poms.
Pom of Project A
   <project>
      ...
      <dependencies>
        <dependency>
          <groupId>it.blogspot.informaticaamodomio</groupId>
          <artifactId>my-artifact-b</artifactId>
          <!-- This is not a jar dependency, so we must specify type. -->
          <type>war</type>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
        </dependency>
      </dependencies>
    </project>
Pom of Project B
    <project>
      ...
      <dependencies>
        <dependency>
          <groupId>it.blogspot.informaticaamodomio</groupId>
          <artifactId>my-artifact-b</artifactId>
          <!-- This is not a jar dependency, so we must specify type. -->
          <type>war</type>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
        </dependency>
      </dependencies>
    </project>

Internal Links

No comments:

Post a Comment

Welcome

Hello everybody, Welcome in my blog called "Information technology archive". Obviously the topics will be related to Informatio...