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

[MWAR-455] Allow force overwrite of read only resources in exploded war target directory #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
19 changes: 17 additions & 2 deletions src/main/java/org/apache/maven/plugins/war/AbstractWarMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public abstract class AbstractWarMojo
@Parameter
private Resource[] webResources;

/**
* If resources should be overwritten, even if they are read only
*/
@Parameter( defaultValue = "false" )
boolean forceOverwriteResources;
Copy link
Member

@olamy olamy Jul 21, 2022

Choose a reason for hiding this comment

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

we usually add @since for new parameter and make the field private.

Copy link
Author

Choose a reason for hiding this comment

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

I can get that added to the pull request. However, based on @michael-o 's response, is that a vain exercise?

/**
* Filters (property files) to include during the interpolation of the pom.xml.
*/
Expand Down Expand Up @@ -533,7 +538,7 @@ public void buildWebapp( MavenProject mavenProject, File webapplicationDirectory
new DefaultWarPackagingContext( webapplicationDirectory, structure, overlayManager, defaultFilterWrappers,
getNonFilteredFileExtensions(), filteringDeploymentDescriptors,
this.artifactFactory, resourceEncoding, propertiesEncoding, useJvmChmod,
failOnMissingWebXml, outputTimestamp );
failOnMissingWebXml, outputTimestamp, forceOverwriteResources );

final List<WarPackagingTask> packagingTasks = getPackagingTasks( overlayManager );

Expand Down Expand Up @@ -608,6 +613,8 @@ private class DefaultWarPackagingContext
private final Collection<String> outdatedResources;

private final String outputTimestamp;

private final boolean forceOverwriteResources;

/**
* @param webappDirectory The web application directory.
Expand All @@ -629,7 +636,8 @@ private class DefaultWarPackagingContext
List<String> nonFilteredFileExtensions,
boolean filteringDeploymentDescriptors, ArtifactFactory artifactFactory,
String resourceEncoding, String propertiesEncoding, boolean useJvmChmod,
final Boolean failOnMissingWebXml, String outputTimestamp )
final Boolean failOnMissingWebXml, String outputTimestamp,
boolean forceOverwriteResources )
{
this.webappDirectory = webappDirectory;
this.webappStructure = webappStructure;
Expand Down Expand Up @@ -701,6 +709,7 @@ public FileVisitResult visitFile( Path file, BasicFileAttributes attrs )
}
}
this.outputTimestamp = outputTimestamp;
this.forceOverwriteResources = forceOverwriteResources;
}

protected boolean checkAllPathsForOutdated()
Expand Down Expand Up @@ -891,6 +900,12 @@ public String getOutputTimestamp()
{
return outputTimestamp;
}

@Override
public boolean isForceOverwriteResources()
{
return forceOverwriteResources;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ protected boolean copyFile( WarPackagingContext context, File source, File desti
}
else
{
if ( context.isForceOverwriteResources() && destination.exists() && !destination.canWrite() )
{
destination.setWritable( true );
}
FileUtils.copyFile( source.getCanonicalFile(), destination );
// preserve timestamp
destination.setLastModified( readAttributes.lastModifiedTime().toMillis() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,10 @@ public interface WarPackagingContext
* @since 3.3.0
*/
String getOutputTimestamp();

/**
* If files are modified, but the target is readonly, force update.
* @return forceOverwriteResources
*/
boolean isForceOverwriteResources();
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,65 @@ public void testSimpleExplodedWarWTargetPath()
expectedWebResourceFile.delete();
}

/**
* @throws Exception in case of an error.
*/
public void testReadOnlyFileInDestinationInExplodedWar()
throws Exception
{
// setup test data
String testId = "SimpleExplodedWar";
MavenProjectBasicStub project = new MavenProjectBasicStub();
File webAppSource = createWebAppSource( testId );
File classesDir = createClassesDir( testId, false );
File webAppResource = new File( getTestDirectory(), testId + "-resources" );
File webAppDirectory = new File( getTestDirectory(), testId );
File sampleResource = new File( webAppResource, "pix/panis_na.jpg" );
ResourceStub[] resources = new ResourceStub[] { new ResourceStub() };


createFile( sampleResource );

File conflictingFile = new File( webAppDirectory, "pix/panis_na.jpg" );
if (conflictingFile.exists() )
{
conflictingFile.delete();
}

createFile( conflictingFile );
conflictingFile.setWritable(false);
conflictingFile.setLastModified(System.currentTimeMillis()-100_000);

assertTrue( "sampeResource not found", sampleResource.exists() );

// configure mojo
resources[0].setDirectory( webAppResource.getAbsolutePath() );
mojo.forceOverwriteResources=true;

this.configureMojo( mojo, new LinkedList<String>(), classesDir, webAppSource, webAppDirectory, project );
setVariableValueToObject( mojo, "webResources", resources );
mojo.execute();

// validate operation
File expectedWebSourceFile = new File( webAppDirectory, "pansit.jsp" );
File expectedWebSource2File = new File( webAppDirectory, "org/web/app/last-exile.jsp" );
File expectedWebResourceFile = new File( webAppDirectory, "pix/panis_na.jpg" );
File expectedWEBINFDir = new File( webAppDirectory, "WEB-INF" );
File expectedMETAINFDir = new File( webAppDirectory, "META-INF" );

assertTrue( "source files not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists() );
assertTrue( "source files not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists() );
assertTrue( "resources doesn't exist: " + expectedWebResourceFile, expectedWebResourceFile.exists() );
assertTrue( "WEB-INF not found", expectedWEBINFDir.exists() );
assertTrue( "META-INF not found", expectedMETAINFDir.exists() );

// house keeping
expectedWebSourceFile.delete();
expectedWebSource2File.delete();
expectedWebResourceFile.delete();
conflictingFile.delete();
}

/**
* @throws Exception in case of an error.
*/
Expand Down