MartinKanters commented on a change in pull request #342: URL: https://github.com/apache/maven/pull/342#discussion_r429750167
########## File path: maven-core/src/main/java/org/apache/maven/execution/DefaultBuildResumptionManager.java ########## @@ -0,0 +1,308 @@ +package org.apache.maven.execution; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +import com.google.common.annotations.VisibleForTesting; +import org.apache.commons.lang3.StringUtils; +import org.apache.maven.lifecycle.LifecycleExecutionException; +import org.apache.maven.model.Dependency; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.logging.Logger; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.Properties; +import java.util.stream.Collectors; + +import static java.util.Comparator.comparing; + +/** + * This implementation of {@link BuildResumptionManager} persists information in a properties file. The file is stored + * in the build output directory under the Maven execution root. + */ +@Named +@Singleton +public class DefaultBuildResumptionManager implements BuildResumptionManager +{ + private static final String RESUME_PROPERTIES_FILENAME = "resume.properties"; + private static final String RESUME_FROM_PROPERTY = "resumeFrom"; + private static final String EXCLUDED_PROJECTS_PROPERTY = "excludedProjects"; + private static final String PROPERTY_DELIMITER = ", "; + + @Inject + private Logger logger; + + @Override + public boolean persistResumptionData( MavenExecutionResult result, MavenProject rootProject ) + { + Properties properties = determineResumptionProperties( result ); + + if ( properties.isEmpty() ) + { + logger.debug( "Will not create " + RESUME_PROPERTIES_FILENAME + " file: nothing to resume from" ); + return false; + } + + return writeResumptionFile( rootProject, properties ); + } + + @Override + public void applyResumptionData( MavenExecutionRequest request, MavenProject rootProject ) + { + Properties properties = loadResumptionFile( rootProject.getBuild().getDirectory() ); + applyResumptionProperties( request, properties ); + } + + @Override + public void removeResumptionData( MavenProject rootProject ) + { + Path resumeProperties = Paths.get( rootProject.getBuild().getDirectory(), RESUME_PROPERTIES_FILENAME ); + try + { + Files.deleteIfExists( resumeProperties ); + } + catch ( IOException e ) + { + logger.warn( "Could not delete " + RESUME_PROPERTIES_FILENAME + " file. ", e ); + } + } + + @Override + public String getResumeFromSelector( List<MavenProject> mavenProjects, MavenProject failedProject ) + { + boolean hasOverlappingArtifactId = mavenProjects.stream() + .filter( project -> failedProject.getArtifactId().equals( project.getArtifactId() ) ) + .count() > 1; + + if ( hasOverlappingArtifactId ) + { + return failedProject.getGroupId() + ":" + failedProject.getArtifactId(); + } + + return ":" + failedProject.getArtifactId(); + } + + @VisibleForTesting + Properties determineResumptionProperties( MavenExecutionResult result ) + { + Properties properties = new Properties(); + + List<MavenProject> failedProjects = getFailedProjectsInOrder( result ); + if ( !failedProjects.isEmpty() ) + { + MavenProject resumeFromProject = failedProjects.get( 0 ); + Optional<String> resumeFrom = getResumeFrom( result, resumeFromProject ); + Optional<String> projectsToSkip = determineProjectsToSkip( result, failedProjects, resumeFromProject ); + + resumeFrom.ifPresent( value -> properties.setProperty( RESUME_FROM_PROPERTY, value ) ); + projectsToSkip.ifPresent( value -> properties.setProperty( EXCLUDED_PROJECTS_PROPERTY, value ) ); + } + else + { + logger.warn( "Could not create " + RESUME_PROPERTIES_FILENAME + " file: no failed projects found" ); + } + + return properties; + } + + private List<MavenProject> getFailedProjectsInOrder( MavenExecutionResult result ) + { + List<MavenProject> sortedProjects = result.getTopologicallySortedProjects(); + + return result.getExceptions().stream() + .filter( LifecycleExecutionException.class::isInstance ) + .map( LifecycleExecutionException.class::cast ) + .map( LifecycleExecutionException::getProject ) + .sorted( comparing( sortedProjects::indexOf ) ) + .collect( Collectors.toList() ); + } + + /** + * Determine the project where the next build can be resumed from. + * If the failed project is the first project of the build, + * it does not make sense to use --resume-from, so the result will be empty. + * @param result The result of the Maven build. + * @param failedProject The first failed project of the build. + * @return An optional containing the resume-from suggestion. + */ + private Optional<String> getResumeFrom( MavenExecutionResult result, MavenProject failedProject ) + { + List<MavenProject> allSortedProjects = result.getTopologicallySortedProjects(); + if ( !allSortedProjects.get( 0 ).equals( failedProject ) ) + { + return Optional.of( String.format( "%s:%s", failedProject.getGroupId(), failedProject.getArtifactId() ) ); Review comment: I'm fine to replace format with string concat, I don't think it matters much in the sake of readability. The inconsistency of `getResumeFromSelector()` is on purpose. I think that method was designed in order for the user to get the most short-hand hint to "resume-from" the build with. In this case the user does not have to see this selector at all, so we can use the full unique key (group+artifact id). The added benefit is that we do not have to calculate whether an artifact-id alone is unique in the whole project. ########## File path: maven-core/src/main/java/org/apache/maven/execution/DefaultBuildResumptionManager.java ########## @@ -0,0 +1,308 @@ +package org.apache.maven.execution; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +import com.google.common.annotations.VisibleForTesting; +import org.apache.commons.lang3.StringUtils; +import org.apache.maven.lifecycle.LifecycleExecutionException; +import org.apache.maven.model.Dependency; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.logging.Logger; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.Properties; +import java.util.stream.Collectors; + +import static java.util.Comparator.comparing; + +/** + * This implementation of {@link BuildResumptionManager} persists information in a properties file. The file is stored + * in the build output directory under the Maven execution root. + */ +@Named +@Singleton +public class DefaultBuildResumptionManager implements BuildResumptionManager +{ + private static final String RESUME_PROPERTIES_FILENAME = "resume.properties"; + private static final String RESUME_FROM_PROPERTY = "resumeFrom"; + private static final String EXCLUDED_PROJECTS_PROPERTY = "excludedProjects"; + private static final String PROPERTY_DELIMITER = ", "; + + @Inject + private Logger logger; + + @Override + public boolean persistResumptionData( MavenExecutionResult result, MavenProject rootProject ) + { + Properties properties = determineResumptionProperties( result ); + + if ( properties.isEmpty() ) + { + logger.debug( "Will not create " + RESUME_PROPERTIES_FILENAME + " file: nothing to resume from" ); + return false; + } + + return writeResumptionFile( rootProject, properties ); + } + + @Override + public void applyResumptionData( MavenExecutionRequest request, MavenProject rootProject ) + { + Properties properties = loadResumptionFile( rootProject.getBuild().getDirectory() ); + applyResumptionProperties( request, properties ); + } + + @Override + public void removeResumptionData( MavenProject rootProject ) + { + Path resumeProperties = Paths.get( rootProject.getBuild().getDirectory(), RESUME_PROPERTIES_FILENAME ); + try + { + Files.deleteIfExists( resumeProperties ); + } + catch ( IOException e ) + { + logger.warn( "Could not delete " + RESUME_PROPERTIES_FILENAME + " file. ", e ); + } + } + + @Override + public String getResumeFromSelector( List<MavenProject> mavenProjects, MavenProject failedProject ) + { + boolean hasOverlappingArtifactId = mavenProjects.stream() + .filter( project -> failedProject.getArtifactId().equals( project.getArtifactId() ) ) + .count() > 1; + + if ( hasOverlappingArtifactId ) + { + return failedProject.getGroupId() + ":" + failedProject.getArtifactId(); + } + + return ":" + failedProject.getArtifactId(); + } + + @VisibleForTesting + Properties determineResumptionProperties( MavenExecutionResult result ) + { + Properties properties = new Properties(); + + List<MavenProject> failedProjects = getFailedProjectsInOrder( result ); + if ( !failedProjects.isEmpty() ) + { + MavenProject resumeFromProject = failedProjects.get( 0 ); + Optional<String> resumeFrom = getResumeFrom( result, resumeFromProject ); + Optional<String> projectsToSkip = determineProjectsToSkip( result, failedProjects, resumeFromProject ); + + resumeFrom.ifPresent( value -> properties.setProperty( RESUME_FROM_PROPERTY, value ) ); + projectsToSkip.ifPresent( value -> properties.setProperty( EXCLUDED_PROJECTS_PROPERTY, value ) ); + } + else + { + logger.warn( "Could not create " + RESUME_PROPERTIES_FILENAME + " file: no failed projects found" ); + } + + return properties; + } + + private List<MavenProject> getFailedProjectsInOrder( MavenExecutionResult result ) + { + List<MavenProject> sortedProjects = result.getTopologicallySortedProjects(); + + return result.getExceptions().stream() + .filter( LifecycleExecutionException.class::isInstance ) + .map( LifecycleExecutionException.class::cast ) + .map( LifecycleExecutionException::getProject ) + .sorted( comparing( sortedProjects::indexOf ) ) + .collect( Collectors.toList() ); + } + + /** + * Determine the project where the next build can be resumed from. + * If the failed project is the first project of the build, + * it does not make sense to use --resume-from, so the result will be empty. + * @param result The result of the Maven build. + * @param failedProject The first failed project of the build. + * @return An optional containing the resume-from suggestion. + */ + private Optional<String> getResumeFrom( MavenExecutionResult result, MavenProject failedProject ) + { + List<MavenProject> allSortedProjects = result.getTopologicallySortedProjects(); + if ( !allSortedProjects.get( 0 ).equals( failedProject ) ) + { + return Optional.of( String.format( "%s:%s", failedProject.getGroupId(), failedProject.getArtifactId() ) ); Review comment: I'm fine to replace format with string concat, I don't think it matters much in the sake of readability. The inconsistency of `getResumeFromSelector()` is on purpose. I think that method was designed in order for the user to get the most short-hand hint to "resume-from" the build with. In this case the user does not have to see this selector at all, so we can use the full unique key (group+artifact id). The benefit is that we do not have to calculate whether an artifact-id alone is unique in the whole project. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
