diff --git a/pom.xml b/pom.xml index 835c926..6eba4eb 100644 --- a/pom.xml +++ b/pom.xml @@ -293,6 +293,7 @@ **/CHANGELOG src/quality/** src/doc/images/** + src/it/**/mlr/** src/it/resources/** src/it/**/invoker.properties src/test/resources/** diff --git a/src/it/issues/issue-146/.gitignore b/src/it/issues/issue-146/.gitignore new file mode 100644 index 0000000..d266c04 --- /dev/null +++ b/src/it/issues/issue-146/.gitignore @@ -0,0 +1,4 @@ +prebuild.log +build.log +mlr/ +target/ \ No newline at end of file diff --git a/src/it/issues/issue-146/.mvn/extensions.xml b/src/it/issues/issue-146/.mvn/extensions.xml new file mode 100644 index 0000000..0183989 --- /dev/null +++ b/src/it/issues/issue-146/.mvn/extensions.xml @@ -0,0 +1,25 @@ + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + \ No newline at end of file diff --git a/src/it/issues/issue-146/.mvn/jgitver.config.xml b/src/it/issues/issue-146/.mvn/jgitver.config.xml new file mode 100644 index 0000000..be0d563 --- /dev/null +++ b/src/it/issues/issue-146/.mvn/jgitver.config.xml @@ -0,0 +1,22 @@ + + + CONFIGURABLE + \ No newline at end of file diff --git a/src/it/issues/issue-146/invoker.properties b/src/it/issues/issue-146/invoker.properties new file mode 100644 index 0000000..ff194fd --- /dev/null +++ b/src/it/issues/issue-146/invoker.properties @@ -0,0 +1,2 @@ +# invoker.mavenOpts = -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +invoker.goals=clean install -Dmaven.repo.local=${basedir}/src/it/issues/issue-146/mlr diff --git a/src/it/issues/issue-146/pom.xml b/src/it/issues/issue-146/pom.xml new file mode 100644 index 0000000..243df66 --- /dev/null +++ b/src/it/issues/issue-146/pom.xml @@ -0,0 +1,55 @@ + + + + 4.0.0 + + fr.brouillard.oss.it + issue-146 + 0 + pom + + Verify MLR is excluded + + + UTF-8 + + + + + + maven-antrun-plugin + 3.0.0 + + + validate + + run + + + + used version: ${jgitver.used_version} + + + + + + + + diff --git a/src/it/issues/issue-146/prebuild.groovy b/src/it/issues/issue-146/prebuild.groovy new file mode 100644 index 0000000..a41e250 --- /dev/null +++ b/src/it/issues/issue-146/prebuild.groovy @@ -0,0 +1,50 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver-maven-plugin] (matthieu@brouillard.fr) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +def log = new PrintWriter( new File(basedir, "prebuild.log").newWriter("UTF-8"), true ) +log.println( "Prebuild started at: " + new Date() + " in: " + basedir ) + +[ + "rm -rf mlr", + "cp -r ../../../../target/local-repo mlr", + "git --version", + "rm -rf .git", + "git init", + "git config user.name nobody", + "git config user.email nobody@nowhere.com", + "dd if=/dev/urandom of=content bs=512 count=2", + "git add .", + "git commit --message=initial_commit", + "git tag -a 1.0.0 --message=release_1.0.0", + "git status", + "git log --graph --oneline" +].each{ command -> + + def proc = command.execute(null, basedir) + def sout = new StringBuilder(), serr = new StringBuilder() + proc.waitForProcessOutput(sout, serr) + + log.println( "cmd: " + command ) + log.println( "out:" ) ; log.println( sout.toString().trim() ) + log.println( "err:" ) ; log.println( serr.toString().trim() ) + log.println( "ret: " + proc.exitValue() ) + + assert proc.exitValue() == 0 + +} + +log.println( "Prebuild completed at: " + new Date() ) +log.close() +return true diff --git a/src/it/issues/issue-146/verify.groovy b/src/it/issues/issue-146/verify.groovy new file mode 100644 index 0000000..368af0c --- /dev/null +++ b/src/it/issues/issue-146/verify.groovy @@ -0,0 +1,46 @@ +/** + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver-maven-plugin] (matthieu@brouillard.fr) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +def log = new PrintWriter( new File(basedir, "verify.log").newWriter("UTF-8"), true ) +log.println( "Verify started at: " + new Date() + " in: " + basedir ) + +[ + "chmod -R 755 .git", + "rm -rf .git" +].each{ command -> + + def proc = command.execute(null, basedir) + def sout = new StringBuilder(), serr = new StringBuilder() + proc.waitForProcessOutput(sout, serr) + + log.println( "cmd: " + command ) + log.println( "out:" ) ; log.println( sout.toString().trim() ) + log.println( "err:" ) ; log.println( serr.toString().trim() ) + log.println( "ret: " + proc.exitValue() ) + + assert proc.exitValue() == 0 + +} + +def buildLog = new File(basedir, "build.log").readLines() + +// Check the version was used by the plugin execution +def versionChanges = buildLog.findAll { it =~ /fr.brouillard.oss.it::issue-146::0 -> 1.0.0/ } +log.println( "versionChanges: " + versionChanges ) +assert 0 < versionChanges.size() + +log.println( "Verify completed at: " + new Date() ) +log.close() +return true diff --git a/src/main/java/fr/brouillard/oss/jgitver/JGitverConfigurationComponent.java b/src/main/java/fr/brouillard/oss/jgitver/JGitverConfigurationComponent.java index 747536e..3055ed0 100644 --- a/src/main/java/fr/brouillard/oss/jgitver/JGitverConfigurationComponent.java +++ b/src/main/java/fr/brouillard/oss/jgitver/JGitverConfigurationComponent.java @@ -19,22 +19,22 @@ import fr.brouillard.oss.jgitver.cfg.ConfigurationLoader; import java.io.File; import java.io.IOException; +import java.util.Collection; import java.util.LinkedList; import java.util.List; +import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; import org.apache.maven.MavenExecutionException; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.plugin.LegacySupport; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.logging.Logger; @Component(role = JGitverConfiguration.class, instantiationStrategy = "singleton") public class JGitverConfigurationComponent implements JGitverConfiguration { - @Requirement private LegacySupport legacySupport = null; - @Requirement private Logger logger = null; + @Requirement private JGitverExecutionInformationProvider executionInformationProvider; + private volatile Configuration configuration; private List excludedDirectories = new LinkedList<>(); @@ -44,15 +44,18 @@ public Configuration getConfiguration() throws MavenExecutionException { if (configuration == null) { synchronized (this) { if (configuration == null) { - MavenSession mavenSession = legacySupport.getSession(); - final File rootDirectory = mavenSession.getRequest().getMultiModuleProjectDirectory(); + final File rootDirectory = executionInformationProvider.getRootDirectory(); logger.debug( "using " + JGitverUtils.EXTENSION_PREFIX + " on directory: " + rootDirectory); - configuration = ConfigurationLoader.loadFromRoot(rootDirectory, logger); + configuration = new ConfigurationLoader(rootDirectory, logger).load(); - initFromRootDirectory(rootDirectory, configuration.exclusions); + excludedDirectories.add( + new File(executionInformationProvider.getLocalRepository().getBasedir())); + excludedDirectories.addAll( + computeExcludedDirectoriesFromConfigurationExclusions( + rootDirectory, configuration.exclusions)); } } } @@ -60,14 +63,19 @@ public Configuration getConfiguration() throws MavenExecutionException { return configuration; } - private void initFromRootDirectory(File rootDirectory, List exclusions) { - exclusions.stream() - .map(dirName -> new File(rootDirectory, dirName)) - .forEach( - dir -> { - excludedDirectories.add(dir); - logger.debug("ignoring directory (& sub dirs): " + dir); - }); + private Collection computeExcludedDirectoriesFromConfigurationExclusions( + File rootDirectory, List exclusions) { + return exclusions.stream() + .map( + dirName -> { + File directory = new File(dirName); + if (!directory.isAbsolute()) { + directory = new File(rootDirectory, dirName); + } + logger.debug("ignoring directory (& sub dirs): " + directory); + return directory; + }) + .collect(Collectors.toList()); } @Override diff --git a/src/main/java/fr/brouillard/oss/jgitver/JGitverExecutionInformationProvider.java b/src/main/java/fr/brouillard/oss/jgitver/JGitverExecutionInformationProvider.java new file mode 100644 index 0000000..8846d82 --- /dev/null +++ b/src/main/java/fr/brouillard/oss/jgitver/JGitverExecutionInformationProvider.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver-maven-plugin] (matthieu@brouillard.fr) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package fr.brouillard.oss.jgitver; + +import java.io.File; +import java.util.Optional; +import org.apache.maven.artifact.repository.ArtifactRepository; + +public interface JGitverExecutionInformationProvider { + + ArtifactRepository getLocalRepository(); + + void setLocalRepository(ArtifactRepository localRepository); + + File getRootDirectory(); + + void setRootDirectory(File rootDirectory); + + void setSession(JGitverSession session); + + Optional session(); +} diff --git a/src/main/java/fr/brouillard/oss/jgitver/JGitverExecutionInformationProviderComponent.java b/src/main/java/fr/brouillard/oss/jgitver/JGitverExecutionInformationProviderComponent.java new file mode 100644 index 0000000..3914701 --- /dev/null +++ b/src/main/java/fr/brouillard/oss/jgitver/JGitverExecutionInformationProviderComponent.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver-maven-plugin] (matthieu@brouillard.fr) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package fr.brouillard.oss.jgitver; + +import java.io.File; +import java.util.Optional; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.codehaus.plexus.component.annotations.Component; + +@Component(role = JGitverExecutionInformationProvider.class, instantiationStrategy = "singleton") +public class JGitverExecutionInformationProviderComponent + implements JGitverExecutionInformationProvider { + private ArtifactRepository localRepository; + private File rootDirectory; + private JGitverSession session; + + @Override + public ArtifactRepository getLocalRepository() { + return localRepository; + } + + @Override + public void setLocalRepository(ArtifactRepository localRepository) { + this.localRepository = localRepository; + } + + @Override + public File getRootDirectory() { + return rootDirectory; + } + + @Override + public void setRootDirectory(File rootDirectory) { + this.rootDirectory = rootDirectory; + } + + @Override + public void setSession(JGitverSession session) { + this.session = session; + } + + @Override + public Optional session() { + return Optional.ofNullable(session); + } +} diff --git a/src/main/java/fr/brouillard/oss/jgitver/JGitverExtension.java b/src/main/java/fr/brouillard/oss/jgitver/JGitverExtension.java index 11e046f..3ff134a 100644 --- a/src/main/java/fr/brouillard/oss/jgitver/JGitverExtension.java +++ b/src/main/java/fr/brouillard/oss/jgitver/JGitverExtension.java @@ -43,14 +43,20 @@ public class JGitverExtension extends AbstractMavenLifecycleParticipant { @Requirement private JGitverConfiguration configurationProvider; + @Requirement private JGitverExecutionInformationProvider executionInformationProvider; + @Override public void afterSessionStart(MavenSession mavenSession) throws MavenExecutionException { if (JGitverUtils.shouldSkip(mavenSession)) { logger.info(" jgitver execution has been skipped by request of the user"); sessionHolder.setSession(null); + executionInformationProvider.setSession(null); } else { final File rootDirectory = mavenSession.getRequest().getMultiModuleProjectDirectory(); + executionInformationProvider.setLocalRepository(mavenSession.getLocalRepository()); + executionInformationProvider.setRootDirectory(rootDirectory); + logger.debug("using " + JGitverUtils.EXTENSION_PREFIX + " on directory: " + rootDirectory); Configuration cfg = configurationProvider.getConfiguration(); @@ -138,6 +144,7 @@ public void afterSessionStart(MavenSession mavenSession) throws MavenExecutionEx JGitverSession session = new JGitverSession(infoProvider, rootDirectory); sessionHolder.setSession(session); + executionInformationProvider.setSession(session); } catch (Exception ex) { logger.warn( "cannot autoclose GitVersionCalculator object for project: " + rootDirectory, ex); @@ -148,6 +155,7 @@ public void afterSessionStart(MavenSession mavenSession) throws MavenExecutionEx @Override public void afterSessionEnd(MavenSession session) throws MavenExecutionException { sessionHolder.setSession(null); + executionInformationProvider.setSession(null); } @Override @@ -160,7 +168,6 @@ public void afterProjectsRead(MavenSession mavenSession) throws MavenExecutionEx final Consumer c = cs -> logger.warn(cs.toString()); if (JGitverModelProcessor.class.isAssignableFrom(modelProcessor.getClass())) { - if (!mavenSession .getUserProperties() .containsKey(JGitverUtils.SESSION_MAVEN_PROPERTIES_KEY)) { @@ -170,7 +177,7 @@ public void afterProjectsRead(MavenSession mavenSession) throws MavenExecutionEx JGitverUtils.failAsOldMechanism(c); } - sessionHolder + executionInformationProvider .session() .ifPresent( jgitverSession -> { diff --git a/src/main/java/fr/brouillard/oss/jgitver/JGitverGuiceModule.java b/src/main/java/fr/brouillard/oss/jgitver/JGitverGuiceModule.java index 2f3ad06..c1d6ac0 100644 --- a/src/main/java/fr/brouillard/oss/jgitver/JGitverGuiceModule.java +++ b/src/main/java/fr/brouillard/oss/jgitver/JGitverGuiceModule.java @@ -28,5 +28,7 @@ public class JGitverGuiceModule extends AbstractModule { @Override protected void configure() { bind(JGitverConfiguration.class).to(JGitverConfigurationComponent.class); + bind(JGitverExecutionInformationProvider.class) + .to(JGitverExecutionInformationProviderComponent.class); } } diff --git a/src/main/java/fr/brouillard/oss/jgitver/JGitverModelProcessor.java b/src/main/java/fr/brouillard/oss/jgitver/JGitverModelProcessor.java index 1694143..9780812 100644 --- a/src/main/java/fr/brouillard/oss/jgitver/JGitverModelProcessor.java +++ b/src/main/java/fr/brouillard/oss/jgitver/JGitverModelProcessor.java @@ -63,6 +63,8 @@ public class JGitverModelProcessor extends DefaultModelProcessor { @Requirement private JGitverSessionHolder jgitverSession; + @Requirement private JGitverExecutionInformationProvider executionInformationProvider; + public JGitverModelProcessor() { super(); } @@ -84,7 +86,7 @@ public Model read(InputStream input, Map options) throws IOException private Model provisionModel(Model model, Map options) throws IOException { MavenSession session = legacySupport.getSession(); - Optional optSession = jgitverSession.session(); + Optional optSession = executionInformationProvider.session(); if (!optSession.isPresent()) { // don't do anything in case no jgitver is there (execution could have been skipped) return model;