Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dantran opened this issue Feb 13, 2017 · 3 comments
Open

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

dantran opened this issue Feb 13, 2017 · 3 comments

Comments

@dantran
Copy link

dantran commented Feb 13, 2017

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
Copy link
Contributor

Does the problem affect unit or integration tests?

@dantran
Copy link
Author

dantran commented Feb 13, 2017

@TobiX
Copy link

TobiX commented Feb 5, 2022

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)...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants