Wednesday, 10 July 2019

Maven: Plugins


Maven is a plugin execution framework. Almost all the work is done by maven plugins. A maven plugin provides a set of goals.

Maven plugins are generally used to
a.   Generate artifacts like jar, war and ear from the source code
b.   Run the junit tests.
c.    Install the artifacts into local, remote repositories.
d.    Copy the resources to the output directory for including in the JAR.
e.   Generate reports
f.     Get information about the working environment for the project.

How to execute a plugin goal?
below command is used to execute a plugin goal.

Syntax
mvn [pluginName]:[goalName]

Example
mvn install dependency:copy-dependencies

in the above example 'dependency' is the plugin name and 'copy-dependencies' is the goal name.

How to configure a plugin?
You can configure a plugin directly under <project> element (or) under profile element.

Configure a plugin under <project> element
<project>
  ...
  <build>
    <plugins>
          <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <phase>install</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/lib</outputDirectory>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
  </build>
  ...
</project>


Configure a plugin under profile element
<project>
  ...
  <profiles>
    <profile>
      <id>copyDependencies</id>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <phase>install</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/lib</outputDirectory>
      <excludeScope>test</excludeScope>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>

In my next post, I am going to show you, how to copy dependencies into target/lib directory using maven-dependency-plugin.

Reference





Previous                                                    Next                                                    Home

No comments:

Post a Comment