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

[#502] : delete episodeFile if only file generated in configuration phase of m2e #503

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.apache.maven.project.artifact.InvalidDependencyVersionException;
import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.jvnet.jaxb.maven.net.CompositeURILastModifiedResolver;
Expand All @@ -75,6 +76,7 @@
import org.jvnet.jaxb.maven.util.LocaleUtils;
import org.jvnet.jaxb.maven.util.CollectionUtils.Function;
import org.sonatype.plexus.build.incremental.BuildContext;
import org.sonatype.plexus.build.incremental.EmptyScanner;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -479,6 +481,7 @@ protected void doExecute() throws MojoExecutionException {
setupDirectories();
doExecute(options);
addIfExistsToEpisodeSchemaBindings();
deleteEpisodeIfOnlyFileOnEmptyContext();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to just not call doExecute instead of creating and then deleting the file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alerosmile : there is some scenario (simple ones with no bindings / catalog) where files are just created the right way

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@laurentschoelens : You are right but I once read this:
https://www.eclipse.org/lists/m2e-dev/msg01178.html

maven-resources-plugin:copy-resources does nothing during onConfiguration.

Copy link
Collaborator Author

@laurentschoelens laurentschoelens Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you suggest that the runOnConfiguration should be set to false ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the source roots must be added during runOnConfiguration, but according to Igor's comment, no files should be created. I'm not sure but I think Igor is one of the main creator of m2e.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, it's working as expected with only runOnIncremental 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, it's working as expected with only runOnIncremental 😄

Or not 😄

final BuildContext buildContext = getBuildContext();
getLog().debug(MessageFormat.format("Refreshing the generated directory [{0}].",
getGenerateDirectory().getAbsolutePath()));
Expand Down Expand Up @@ -577,6 +580,33 @@ private void addIfExistsToEpisodeSchemaBindings() throws MojoExecutionException
}
}

private void deleteEpisodeIfOnlyFileOnEmptyContext() {
if (!getEpisode() || getBuildContext() == null) {
return;
}
final File episodeFile = getEpisodeFile();
if (getBuildContext().isIncremental() && getBuildContext().newScanner(getGenerateDirectory()) instanceof EmptyScanner) {
if (!episodeFile.canWrite()) {
getLog().warn(MessageFormat
.format("Episode file [{0}] is not writable, could not delete on incremental empty build.", episodeFile));
return;
}
DirectoryScanner directoryScanner = new DirectoryScanner();
directoryScanner.setBasedir(getGenerateDirectory());
directoryScanner.setIncludes(getProduces());
directoryScanner.scan();

if (directoryScanner.getIncludedFiles() != null && directoryScanner.getIncludedFiles().length == 1) {
String fileFound = directoryScanner.getIncludedFiles()[0];
if (new File(directoryScanner.getBasedir(), fileFound).equals(episodeFile)) {
getLog().info(MessageFormat
.format("directoryScanner got only one file [{0}] which is episodeFile - deleting since running in m2e context.", fileFound, episodeFile));
episodeFile.delete();
}
}
}
}

private URILastModifiedResolver uriLastModifiedResolver;

private void setupURILastModifiedResolver() {
Expand Down
Loading