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

[MBUILDCACHE-105] Executing 'clean process-test-classes' then 'clean verify' leads to empty jars #176

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
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ private CacheResult analyzeResult(CacheContext context, List<MojoExecution> mojo
build.getCacheImplementationVersion());
}

if (lifecyclePhasesHelper.isLaterPhaseThanBuild("package", build)) {
LOGGER.warn("Cached build doesn't include phase 'package', cannot restore");
Copy link
Contributor

@AlexanderAshitkin AlexanderAshitkin Aug 27, 2024

Choose a reason for hiding this comment

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

In the current implementation, the 'analyzeResult' returns partial success in lines 277-278 as expected.

More accurate implementation could support pre-compile phases - 'save' and restore generated sources and continue with compile. This will allow restoring missing sources from the cache and potentially saving time on the code generation. Could be handy in IDEs which could faster compile the project after a branch switch (if generated sources are restored).

The second concern is that we still process builds that will never be used in the 'save'. Save and restore should be consistent, and the situation when cached builds are not usable should also be considered for improvement.

Though overall, as a quick fix, this change seems legitimate, it feels like restoring generated sources and commencing with compilation would be more optimal.

Copy link
Member Author

Choose a reason for hiding this comment

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

@AlexanderAshitkin I agree with you.

At first I wanted to fix the extension to make it able to save cache state on phase < package. But there is this sentence in the documentation:

Once the extension is activated, the cache automatically kicks in on every package or higher phase.

I could not find the reason of this constraint, so I made sure this fix respects the latter. Also the fix should be quick to merge to prevent people from getting into trouble.

I also think this extension should not limit itself to phases >= package. We can still come back to that later.

return failure(build, context);
}

List<MojoExecution> cachedSegment =
lifecyclePhasesHelper.getCachedSegment(context.getProject(), mojoExecutions, build);
List<MojoExecution> missingMojos = build.getMissingExecutions(cachedSegment);
Expand Down
73 changes: 73 additions & 0 deletions src/test/java/org/apache/maven/buildcache/its/Issue105Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.
*/
package org.apache.maven.buildcache.its;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
import org.apache.commons.compress.archivers.jar.JarArchiveInputStream;
import org.apache.maven.buildcache.its.junit.IntegrationTest;
import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* @author Réda Housni Alaoui
*/
@IntegrationTest("src/test/projects/mbuildcache-105")
class Issue105Test {

public static final String BUILT_JAR = "target/simple-0.0.1-SNAPSHOT.jar";

@Test
void simple(Verifier verifier) throws VerificationException, IOException {
verifier.setAutoclean(false);

verifier.setLogFileName("../log-1.txt");
verifier.executeGoals(Arrays.asList("clean", "process-test-classes"));
verifier.verifyErrorFreeLog();

verifier.setLogFileName("../log-2.txt");
verifier.executeGoals(Arrays.asList("clean", "verify"));
verifier.verifyErrorFreeLog();
verifier.verifyTextInLog("Cached build doesn't include phase 'package', cannot restore");
verifier.verifyFilePresent(BUILT_JAR);

Path jarPath = Paths.get(verifier.getBasedir()).resolve(BUILT_JAR);
assertJarEntryExists(jarPath, "org/apache/maven/buildcache/Test.class");
}

private void assertJarEntryExists(Path jarPath, String name) throws IOException {
try (JarArchiveInputStream inputStream = new JarArchiveInputStream(Files.newInputStream(jarPath))) {
JarArchiveEntry entry = inputStream.getNextJarEntry();
while (entry != null) {
if (entry.getName().equals(name)) {
return;
}
entry = inputStream.getNextJarEntry();
}
}
Assertions.fail("No JAR entry found for name '" + name + "'");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!---
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.
-->
<cache xmlns="http://maven.apache.org/BUILD-CACHE-CONFIG/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/BUILD-CACHE-CONFIG/1.0.0 https://maven.apache.org/xsd/build-cache-config-1.0.0.xsd">
</cache>
42 changes: 42 additions & 0 deletions src/test/projects/mbuildcache-105/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!--

Copyright 2021 the original author or authors.

Licensed 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.

-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.caching.test.mbuildcache-105</groupId>
<artifactId>simple</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
<extensions>
<extension>
<groupId>org.apache.maven.extensions</groupId>
<artifactId>maven-build-cache-extension</artifactId>
<version>${projectVersion}</version>
</extension>
</extensions>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.
*/
package org.apache.maven.buildcache;

class Test
{

}