Skip to content

Commit 19f4a0a

Browse files
committed
Save attached outputs for compile-only cache entries
1 parent 8615880 commit 19f4a0a

File tree

12 files changed

+330
-33
lines changed

12 files changed

+330
-33
lines changed

src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -498,25 +498,23 @@ public void save(
498498
final MavenProject project = context.getProject();
499499
final MavenSession session = context.getSession();
500500
try {
501+
attachedResourcesPathsById.clear();
502+
attachedResourceCounter = 0;
503+
501504
final HashFactory hashFactory = cacheConfig.getHashFactory();
505+
final HashAlgorithm algorithm = hashFactory.createAlgorithm();
502506
final org.apache.maven.artifact.Artifact projectArtifact = project.getArtifact();
503-
final List<org.apache.maven.artifact.Artifact> attachedArtifacts;
504-
final List<Artifact> attachedArtifactDtos;
505-
final Artifact projectArtifactDto;
506-
if (project.hasLifecyclePhase("package")) {
507-
final HashAlgorithm algorithm = hashFactory.createAlgorithm();
508-
attachGeneratedSources(project);
509-
attachOutputs(project);
510-
attachedArtifacts = project.getAttachedArtifacts() != null
511-
? project.getAttachedArtifacts()
512-
: Collections.emptyList();
513-
attachedArtifactDtos = artifactDtos(attachedArtifacts, algorithm, project);
514-
projectArtifactDto = artifactDto(project.getArtifact(), algorithm, project);
515-
} else {
516-
attachedArtifacts = Collections.emptyList();
517-
attachedArtifactDtos = new ArrayList<>();
518-
projectArtifactDto = null;
519-
}
507+
final boolean hasPackagePhase = project.hasLifecyclePhase("package");
508+
509+
attachGeneratedSources(project);
510+
attachOutputs(project);
511+
512+
final List<org.apache.maven.artifact.Artifact> attachedArtifacts = project.getAttachedArtifacts() != null
513+
? project.getAttachedArtifacts()
514+
: Collections.emptyList();
515+
final List<Artifact> attachedArtifactDtos = artifactDtos(attachedArtifacts, algorithm, project);
516+
final Artifact projectArtifactDto = hasPackagePhase ? artifactDto(project.getArtifact(), algorithm, project)
517+
: null;
520518

521519
List<CompletedExecution> completedExecution = buildExecutionInfo(mojoExecutions, executionEvents);
522520

@@ -534,22 +532,19 @@ public void save(
534532
localCache.beforeSave(context);
535533

536534
// if package phase presence means new artifacts were packaged
537-
if (project.hasLifecyclePhase("package")) {
538-
if (projectArtifact.getFile() != null) {
539-
localCache.saveArtifactFile(cacheResult, projectArtifact);
540-
}
541-
for (org.apache.maven.artifact.Artifact attachedArtifact : attachedArtifacts) {
542-
if (attachedArtifact.getFile() != null) {
543-
boolean storeArtifact =
544-
isOutputArtifact(attachedArtifact.getFile().getName());
545-
if (storeArtifact) {
546-
localCache.saveArtifactFile(cacheResult, attachedArtifact);
547-
} else {
548-
LOGGER.debug(
549-
"Skipping attached project artifact '{}' = "
550-
+ " it is marked for exclusion from caching",
551-
attachedArtifact.getFile().getName());
552-
}
535+
if (hasPackagePhase && projectArtifact.getFile() != null) {
536+
localCache.saveArtifactFile(cacheResult, projectArtifact);
537+
}
538+
for (org.apache.maven.artifact.Artifact attachedArtifact : attachedArtifacts) {
539+
if (attachedArtifact.getFile() != null) {
540+
boolean storeArtifact = isOutputArtifact(attachedArtifact.getFile().getName());
541+
if (storeArtifact) {
542+
localCache.saveArtifactFile(cacheResult, attachedArtifact);
543+
} else {
544+
LOGGER.debug(
545+
"Skipping attached project artifact '{}' = "
546+
+ " it is marked for exclusion from caching",
547+
attachedArtifact.getFile().getName());
553548
}
554549
}
555550
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.buildcache.its;
20+
21+
import java.util.Arrays;
22+
23+
import org.apache.maven.buildcache.its.junit.IntegrationTest;
24+
import org.apache.maven.it.VerificationException;
25+
import org.apache.maven.it.Verifier;
26+
import org.junit.jupiter.api.Test;
27+
28+
@IntegrationTest("src/test/projects/issue-393-compile-restore")
29+
class Issue393CompileRestoreTest {
30+
31+
@Test
32+
void restoresAttachedOutputsAfterCompileOnlyBuild(Verifier verifier) throws VerificationException {
33+
verifier.setAutoclean(false);
34+
35+
verifier.setLogFileName("../log-compile.txt");
36+
verifier.executeGoals(Arrays.asList("clean", "compile"));
37+
verifier.verifyErrorFreeLog();
38+
verifier.verifyFilePresent("app/target/classes/module-info.class");
39+
verifier.verifyFilePresent("consumer/target/classes/module-info.class");
40+
41+
verifier.setLogFileName("../log-verify.txt");
42+
verifier.executeGoals(Arrays.asList("clean", "verify"));
43+
verifier.verifyErrorFreeLog();
44+
verifier.verifyTextInLog("Found cached build, restoring org.apache.maven.caching.test.jpms:issue-393-app from cache");
45+
verifier.verifyTextInLog("Skipping plugin execution (cached): compiler:compile");
46+
47+
verifier.verifyFilePresent("app/target/classes/module-info.class");
48+
verifier.verifyFilePresent("consumer/target/classes/module-info.class");
49+
verifier.verifyFilePresent("consumer/target/test-classes/org/apache/maven/caching/test/jpms/consumer/ConsumerTest.class");
50+
}
51+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to You under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
21+
-->
22+
<extensions>
23+
<extension>
24+
<groupId>org.apache.maven.extensions</groupId>
25+
<artifactId>maven-build-cache-extension</artifactId>
26+
<version>${projectVersion}</version>
27+
</extension>
28+
</extensions>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to You under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<cache xmlns="http://maven.apache.org/BUILD-CACHE-CONFIG/1.2.0"
23+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
24+
xsi:schemaLocation="http://maven.apache.org/BUILD-CACHE-CONFIG/1.2.0 https://maven.apache.org/xsd/build-cache-config-1.2.0.xsd">
25+
<configuration>
26+
<attachedOutputs>
27+
<dirNames>
28+
<dirName>classes</dirName>
29+
<dirName>test-classes</dirName>
30+
<dirName>maven-status</dirName>
31+
</dirNames>
32+
</attachedOutputs>
33+
</configuration>
34+
</cache>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!--
2+
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to You under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
20+
-->
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
25+
<modelVersion>4.0.0</modelVersion>
26+
27+
<parent>
28+
<groupId>org.apache.maven.caching.test.jpms</groupId>
29+
<artifactId>issue-393-compile-restore</artifactId>
30+
<version>0.0.1-SNAPSHOT</version>
31+
</parent>
32+
33+
<artifactId>issue-393-app</artifactId>
34+
<name>Issue 393 Compile Restore - App Module</name>
35+
<packaging>jar</packaging>
36+
37+
</project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module org.apache.maven.caching.test.jpms.app {
2+
exports org.apache.maven.caching.test.jpms.app;
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.apache.maven.caching.test.jpms.app;
2+
3+
public final class Greeting {
4+
5+
private Greeting() {
6+
// utility
7+
}
8+
9+
public static String message() {
10+
return "hello from module";
11+
}
12+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<!--
2+
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to You under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
20+
-->
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
25+
<modelVersion>4.0.0</modelVersion>
26+
27+
<parent>
28+
<groupId>org.apache.maven.caching.test.jpms</groupId>
29+
<artifactId>issue-393-compile-restore</artifactId>
30+
<version>0.0.1-SNAPSHOT</version>
31+
</parent>
32+
33+
<artifactId>issue-393-consumer</artifactId>
34+
<name>Issue 393 Compile Restore - Consumer Module</name>
35+
<packaging>jar</packaging>
36+
37+
<dependencies>
38+
<dependency>
39+
<groupId>${project.groupId}</groupId>
40+
<artifactId>issue-393-app</artifactId>
41+
<version>${project.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.junit.jupiter</groupId>
45+
<artifactId>junit-jupiter</artifactId>
46+
<version>5.10.2</version>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-surefire-plugin</artifactId>
56+
<version>3.2.5</version>
57+
<configuration>
58+
<useModulePath>true</useModulePath>
59+
</configuration>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
64+
</project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module org.apache.maven.caching.test.jpms.consumer {
2+
requires org.apache.maven.caching.test.jpms.app;
3+
exports org.apache.maven.caching.test.jpms.consumer;
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.apache.maven.caching.test.jpms.consumer;
2+
3+
import org.apache.maven.caching.test.jpms.app.Greeting;
4+
5+
public final class Consumer {
6+
7+
private Consumer() {
8+
// utility
9+
}
10+
11+
public static String message() {
12+
return Greeting.message();
13+
}
14+
}

0 commit comments

Comments
 (0)