takari / takari-plugin-testing-project

Maven Plugin Testing Framework
Eclipse Public License 1.0
27 stars 19 forks source link

plugin test does not pickup global settings from command line #17

Open dantran opened 7 years ago

dantran commented 7 years ago

My maven plugin builds inside docker container where I specify both maven-user and maven-global settings files. During plugin test, takari-test-testing seems to be aware the present of my maven-user settings file ( ie -s xxxx) and ignore my global settings ( -gs).

Of course, I have a workaround, but it would be best to fix it here for completeness

ifedorenko commented 7 years ago

Does the problem affect unit or integration tests?

dantran commented 7 years ago

My plugin test uses the same pattern as this

https://github.com/mojohaus/wagon-maven-plugin/blob/master/src/test/java/org/codehaus/mojo/wagon/WagonMojoHttpTest.java

TobiX commented 2 years ago

I have this problem with integration tests. I did some half-hearted workarounds over the years, but since recent security hardening measures at my company I switched to the following hack/workaround/implementation:

In the pom.xml, I dump the effective settings into a temporary file (which is deleted after the maven run):

      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.13.1</version>
        <executions>
          <execution>
            <id>dump-merged-maven-settings-for-it</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <scripts>
                <script><![CDATA[
import java.nio.file.Files
def target = java.nio.file.Paths.get(project.build.directory)
def mergedSettings = Files.createTempFile(target, "merged-maven-settings", ".xml")
Files.newBufferedWriter(mergedSettings).with {
    new org.apache.maven.settings.io.xpp3.SettingsXpp3Writer ().write (it, session.settings)
}
project.properties['testSystemSettingsFile'] = mergedSettings.toString()
mergedSettings.toFile().deleteOnExit()
]]></script>
              </scripts>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-ant</artifactId>
            <version>2.4.21</version>
            <scope>compile</scope>
          </dependency>
        </dependencies>
      </plugin>

The property needs to be carried over to failsafe:

      <plugin>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>3.0.0-M5</version>
        <configuration>
          <systemPropertyVariables>
            <testSystemSettingsFile>${testSystemSettingsFile}</testSystemSettingsFile>
          </systemPropertyVariables>
        </configuration>
      </plugin>

And lastly, this needs to be picked up by the integration tests, I derive all tests from this base class:

import io.takari.maven.testing.TestResources;
import io.takari.maven.testing.executor.MavenRuntime;
import io.takari.maven.testing.executor.MavenRuntime.MavenRuntimeBuilder;
import io.takari.maven.testing.executor.MavenVersions;
import io.takari.maven.testing.executor.junit.MavenJUnitTestRunner;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.junit.Rule;
import org.junit.runner.RunWith;

@RunWith(MavenJUnitTestRunner.class)
@MavenVersions({"3.5.3", "3.8.4"})
public class BaseMavenIT {

  public final MavenRuntime maven;

  protected BaseMavenIT(MavenRuntimeBuilder mavenBuilder) throws Exception {
    String settingsFileProperty = System.getProperty("testSystemSettingsFile");
    if (settingsFileProperty != null && Files.exists(Paths.get(settingsFileProperty))) {
      mavenBuilder.withCliOptions("--global-settings", settingsFileProperty);
    }
    this.maven = mavenBuilder.withCliOptions("--batch-mode", "--errors").build();
  }
}

This is... workable. Unfortunately, it seems to need this convoluted, since Maven doesn't provide a convenient way for plugins to know the original global and local settings files (at least none that I could find). I would be happier if I didn't have to write all the settings into an extra file (which probably contains repository secrets)...