-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Remove Dev UI and other Dev Mode classes from prod #44250
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
core/deployment/src/main/java/io/quarkus/deployment/builditem/DevModeCleanupBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.quarkus.deployment.builditem; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
|
||
/** | ||
* This allows you to define one or more packages or set of classes that should be removed on Prod Build | ||
*/ | ||
public final class DevModeCleanupBuildItem extends MultiBuildItem { | ||
private final Set<Class> classes; | ||
private final Set<String> packageNames; | ||
|
||
public DevModeCleanupBuildItem(Class c) { | ||
this(c, false); | ||
} | ||
|
||
public DevModeCleanupBuildItem(Class c, boolean wholePackage) { | ||
this(Set.of(c), wholePackage); | ||
} | ||
|
||
public DevModeCleanupBuildItem(Set<Class> c, boolean wholePackage) { | ||
super(); | ||
if (wholePackage) { | ||
this.classes = null; | ||
this.packageNames = c.stream() | ||
.map(clazz -> clazz.getPackage().getName()) | ||
.collect(Collectors.toSet()); | ||
} else { | ||
this.classes = c; | ||
this.packageNames = null; | ||
} | ||
} | ||
|
||
public DevModeCleanupBuildItem(Set<Class> classes) { | ||
super(); | ||
this.classes = classes; | ||
this.packageNames = null; | ||
} | ||
|
||
public DevModeCleanupBuildItem(String... packageName) { | ||
super(); | ||
this.packageNames = Set.of(packageName); | ||
this.classes = null; | ||
|
||
} | ||
|
||
public Set<String> getPackageNames() { | ||
return packageNames; | ||
} | ||
|
||
public Set<Class> getClasses() { | ||
return classes; | ||
} | ||
|
||
public boolean isWholePackage() { | ||
return this.classes == null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
core/deployment/src/main/java/io/quarkus/deployment/dev/DevModeCleanupProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package io.quarkus.deployment.dev; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Enumeration; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
|
||
import io.quarkus.bootstrap.model.ApplicationModel; | ||
import io.quarkus.deployment.IsNormal; | ||
import io.quarkus.deployment.annotations.BuildProducer; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.deployment.builditem.DevModeCleanupBuildItem; | ||
import io.quarkus.deployment.builditem.RemovedResourceBuildItem; | ||
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem; | ||
import io.quarkus.maven.dependency.ResolvedDependency; | ||
|
||
/** | ||
* Cleans up classes that is not needed in Prod | ||
*/ | ||
public class DevModeCleanupProcessor { | ||
|
||
/** | ||
* This makes sure the Dev Mode Classes are not included in the prod build | ||
*/ | ||
@BuildStep(onlyIf = IsNormal.class) | ||
void cleanProd(BuildProducer<RemovedResourceBuildItem> producer, | ||
List<DevModeCleanupBuildItem> devUIItemsToRemove, | ||
CurateOutcomeBuildItem curateOutcomeBuildItem) { | ||
|
||
Set<String> classNames = new HashSet<>(); | ||
Set<String> packageNames = new HashSet<>(); | ||
for (DevModeCleanupBuildItem devUIRemoveItemBuildItem : devUIItemsToRemove) { | ||
if (devUIRemoveItemBuildItem.isWholePackage()) { | ||
packageNames.addAll(devUIRemoveItemBuildItem.getPackageNames()); | ||
} else { | ||
for (Class c : devUIRemoveItemBuildItem.getClasses()) { | ||
classNames.add(c.getName()); | ||
} | ||
} | ||
} | ||
|
||
List<RemovedResourceBuildItem> removedResourceBuildItems = getRemoveResourceBuildItemList(classNames, packageNames, | ||
curateOutcomeBuildItem); | ||
producer.produce(removedResourceBuildItems); | ||
} | ||
|
||
private List<RemovedResourceBuildItem> getRemoveResourceBuildItemList(Set<String> classNames, Set<String> packageNames, | ||
CurateOutcomeBuildItem curateOutcomeBuildItem) { | ||
List<RemovedResourceBuildItem> buildItems = new ArrayList<>(); | ||
|
||
ApplicationModel applicationModel = curateOutcomeBuildItem.getApplicationModel(); | ||
Collection<ResolvedDependency> runtimeDependencies = applicationModel.getRuntimeDependencies(); | ||
List<ResolvedDependency> allArtifacts = new ArrayList<>(runtimeDependencies.size() + 1); | ||
allArtifacts.addAll(runtimeDependencies); | ||
allArtifacts.add(applicationModel.getAppArtifact()); | ||
for (ResolvedDependency i : allArtifacts) { | ||
for (Path path : i.getResolvedPaths()) { | ||
if (path.toString().endsWith(".jar") && path.toFile().isFile()) { | ||
Set<String> foundClasses = new HashSet<>(); | ||
try (JarFile jarFile = new JarFile(path.toFile())) { | ||
Enumeration<JarEntry> entries = jarFile.entries(); | ||
while (entries.hasMoreElements()) { | ||
JarEntry entry = entries.nextElement(); | ||
if (entry.getName().endsWith(".class")) { | ||
String entryClassName = entry.getName() | ||
.replace('/', '.') | ||
.replace(".class", ""); | ||
if (classNames.contains(entryClassName)) { | ||
foundClasses.add(entry.getName()); | ||
} else if (packageNames.stream().anyMatch(entryClassName::startsWith)) { | ||
foundClasses.add(entry.getName()); | ||
} | ||
} | ||
} | ||
} catch (IOException ex) { | ||
throw new UncheckedIOException(ex); | ||
} | ||
if (!foundClasses.isEmpty()) { | ||
buildItems.add(new RemovedResourceBuildItem(path, foundClasses)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return buildItems; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -887,6 +887,19 @@ JsonRPCProvidersBuildItem createJsonRPCServiceForCache() {// <2> | |
|
||
https://github.com/quarkusio/quarkus/blob/main/extensions/cache/deployment/src/main/java/io/quarkus/cache/deployment/devui/CacheDevUiProcessor.java[Example code] | ||
|
||
Also cleanup this in Prod mode, so when the app build in prod mode, we remove all dev-ui related runtime classes: | ||
|
||
[source,java] | ||
---- | ||
@BuildStep(onlyIf = IsNormal.class)// <1> | ||
DevModeCleanupBuildItem cleanProd() {// <2> | ||
return new DevModeCleanupBuildItem(CacheJsonRPCService.class, true);// <3> | ||
} | ||
---- | ||
<1> Always only do this in Normal (Prod) Mode | ||
<2> Produce or return a `DevModeCleanupBuildItem` | ||
<3> Define the class or classes in your runtime module that will be removed. You can also clean the whole package of that class in case you used more than just the JSON-RPC Service | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. show how to clean the whole package as otherwise one is left guessing :) |
||
|
||
Now, in your Runtime module, create the JsonRPC Service. This class will default to an application-scoped bean, except if you explicitly scope the bean. All public methods that return something will be made available to call from the Web component Javascript. | ||
|
||
The return object in these methods can be: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this pops up kinda in the middle in jsonrpc explanation. Maybe have a more explicit section/title like "Remove devUI from Production" ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It only really apply to Dev UI that has jsonrpc services. Many extensions does not have any. Any screen (js) and any build time data or runtime against the deployment cp is already not included in the prod build, it's just when you do the jsonrpc service, hence its added here