0

I have a pom.xml file, and I have commented one dependency and one plugin. After this I refreshed and made reimport, but the plugin works anyway. I can't understand why, because it is absent in the pom.xml.

Actually I try to change output directory for the Surefire Plugin, but it is always target/surefire.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>TEST</groupId>
    <artifactId>TEST</artifactId>
    <version>1.0-SNAPSHOT</version>  

    <properties>
        <!--<org.apache.maven.surefire.version>2.19.1</org.apache.maven.surefire.version>-->
    </properties>

    <!--<reporting>-->
        <!--<plugins>-->
            <!--<plugin>-->
                <!--<groupId>org.apache.maven.plugins</groupId>-->
                <!--<artifactId>maven-surefire-report-plugin</artifactId>-->
                <!--<version>2.19.1</version>-->
            <!--</plugin>-->
        <!--</plugins>-->
    <!--</reporting>-->

    <dependencies>
        <!--<dependency>-->
            <!--<groupId>org.apache.maven.surefire</groupId>-->
            <!--<artifactId>surefire</artifactId>-->
            <!--<version>${org.apache.maven.surefire.version}</version>-->
            <!--<type>pom</type>-->
        <!--</dependency>-->
    </dependencies>

</project>
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Sergey Luchko
  • 2,996
  • 3
  • 31
  • 51

1 Answers1

3

There are several misconceptions with the question. Let's go through them.

Why is a plugin being invoked when I commented a dependency?

Because a <dependency> has no effect at all on the execution of plugins. This is used to declare what your project depends on in order to compile it and run it. It is separate from the actual build process of your project, which is launched by Maven and invokes several plugins to do some tasks, like performing the actual compilation or copying files, etc.

Why is a plugin being invoked when I commented it out?!

The explanation relies on the existence of build lifecycles. To put it simply, a lifecycle consists of several phases, which consists themselves of plugin goals. There are multiple lifecycles possible but the default one is the central one and typically governs the packaging and deployment of the project. The phases are, for example, compile which represents the phase in which the project's sources are being compiled, or package which represents the phase where the project is being packaged into a deliverable, or test which represents the unit-testing of your project.

And, also by default, there are several plugins already bound to phases of the default lifecycle (depending on the packaging). You will notice that there is:

test: surefire:test

which means that, in the test phase of the default lifecycle (for certain packaging), the goal surefire:test is going to be invoked (see also the documentation). This is what you're seeing here: maven-surefire-plugin, whose prefix is surefire and was used above, has a goal called test, whose purpose is to run the test classes of your project.

There's a second consideration. The explanation above refers to build plugins, declared in the <build><plugins><plugin> element of your POM. What you commented out was actually the <reporting> section, which contains the reporting plugins invoked when generating a website for your project.

The actual question

Actually I try to change output directory for surefire plugin

You can do so by configuring the reportsDirectory of the maven-surefire-plugin, which is by default target/surefire-reports. This would look like in your case:

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <build>
    ...
    <plugins>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <reportsDirectory>target/someDirectory</reportsDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Notice how this is placed inside <build> to configure the build plugins. And you can omit the <groupId>org.apache.maven.plugins</groupId>, it is the default.

Best practice is that all generated files during the build (like Surefire reports) are placed inside target, or more generally, the build directory (which can be obtained with the ${project.build.directory} property in a generic manner).

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • How can I refresh maven project to force it forget about plugin?(Refresh build lifecycles) Why is a plugin being invoked when I commented it out?! It is not clear for me, why it so happen. – Sergey Luchko Oct 12 '16 at 19:56
  • @LuchkoSerega Typically, you don't tell Maven to forget about it, but to skip it. Refer to this question to skip tests http://stackoverflow.com/questions/7456006/maven-packaging-without-test-skip-tests. If you really want to disable it completely, refer to this one http://stackoverflow.com/questions/7821152/disable-a-maven-plugin-defined-in-a-parent-pom (the plugin being `maven-surefire-plugin` here) or this one http://stackoverflow.com/a/16329002/1743880 – Tunaki Oct 12 '16 at 20:02
  • Why we can't just remove plugin, as if it was before we added the lines with plugin definition? – Sergey Luchko Oct 12 '16 at 20:06
  • @LuchkoSerega Because the fact that `surefire:test` is invoked in the `test` phase of JAR (for example) is defined in Maven installation itself. It's a pre-cooked definition that you don't need to worry about: you don't want test? skip them with `-DskipTests`. Those default plugin bindings are there because it simplifies things a lot: for example, you don't need to tell Maven to run `maven-compiler-plugin` in the `compile` phase to compile your Java sources: it's already there. – Tunaki Oct 12 '16 at 20:11
  • Thanks a lot!) I noticed surefire in other projects target folder. – Sergey Luchko Oct 12 '16 at 20:36