Skip to content

Commit f8abdb8

Browse files
authored
fix(maven-wrapper): only delete old JAR if performing updates to the properties file (renovatebot#42141)
As reported in renovatebot#41994, in 49fbc83, we assumed that `updateArtifacts` would only ever be called when updating the `maven-wrapper.properties`. As this isn't always the case, and we may be updating a `mvnw` file, we should check what we're about to delete, before we do.
1 parent c80f520 commit f8abdb8

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

lib/modules/manager/maven-wrapper/artifacts.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,42 @@ describe('modules/manager/maven-wrapper/artifacts', () => {
631631
const finalWrittenContent = vi.mocked(fs.writeLocalFile).mock.calls[1][1];
632632
expect(finalWrittenContent).not.toContain('oldhash123');
633633
expect(finalWrittenContent).not.toContain('oldwrapperhash456');
634+
expect(fs.deleteLocalFile).toHaveBeenCalledWith(
635+
'.mvn/wrapper/maven-wrapper.jar',
636+
);
637+
});
638+
639+
it('should not attempt to delete old JAR file if doing a Maven Wrapper update', async () => {
640+
const propertiesWithChecksums = codeBlock`
641+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
642+
distributionSha256Sum=oldhash123
643+
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.3.2/maven-wrapper-3.3.2.jar
644+
wrapperSha256Sum=oldwrapperhash456
645+
`;
646+
647+
httpMock
648+
.scope('https://repo.maven.apache.org')
649+
.get(
650+
'/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip',
651+
)
652+
.reply(200, Buffer.from('fake-maven-content'))
653+
.get(
654+
'/maven2/org/apache/maven/wrapper/maven-wrapper/3.3.2/maven-wrapper-3.3.2.jar',
655+
)
656+
.reply(200, Buffer.from('fake-wrapper-content'));
657+
mockMavenFileChangedInGit();
658+
mockExecAll({ stdout: '', stderr: '' });
659+
// Mock readLocalFile to return content after wrapper:wrapper runs
660+
fs.readLocalFile.mockResolvedValueOnce(propertiesWithChecksums);
661+
662+
await updateArtifacts({
663+
packageFileName: 'mvnw',
664+
newPackageFileContent: propertiesWithChecksums,
665+
updatedDeps: [{ depName: 'maven-wrapper', newValue: '3.3.2' }],
666+
config: { currentValue: '3.3.1', newValue: '3.3.2' },
667+
});
668+
669+
expect(fs.deleteLocalFile).not.toHaveBeenCalled();
634670
});
635671

636672
it('should preserve old checksum when fetch fails', async () => {

lib/modules/manager/maven-wrapper/artifacts.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,13 @@ export async function updateArtifacts({
278278
'maven-wrapper.properties',
279279
'maven-wrapper.jar',
280280
);
281-
try {
282-
await deleteLocalFile(jarPath);
283-
logger.debug({ jarPath }, 'Deleted old maven-wrapper.jar');
284-
} catch {
285-
// File may not exist, ignore
281+
if (jarPath.endsWith('maven-wrapper.jar')) {
282+
try {
283+
await deleteLocalFile(jarPath);
284+
logger.debug({ jarPath }, 'Deleted old maven-wrapper.jar');
285+
} catch {
286+
// File may not exist, ignore
287+
}
286288
}
287289
}
288290

0 commit comments

Comments
 (0)