-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide fix suggestions for not covered Maven plugin execution in pro…
…ject build lifecycle Signed-off-by: Jinbo Wang <[email protected]>
- Loading branch information
1 parent
43630da
commit 35649fd
Showing
6 changed files
with
226 additions
and
1 deletion.
There are no files selected for viewing
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,109 @@ | ||
# Not Covered Maven Plugin Execution | ||
|
||
## Background | ||
Some Maven projects use 3rd party Maven plugins to generate sources or resources, and you may find that the generated code is not picked up by the project classpath, or the Maven goals are not executed by Java extension. This is a known technical issue with `m2e`, the underlying Maven integration tool for Java extension. The reason is that the Maven tooling `m2e` doesn't know if it's safe to run your Maven plugin automatically during a workspace build, so it does not run them by default and requires explicit instructions on how to handle them. Learn more about this issue from the wiki about [Execution Not Covered](https://www.eclipse.org/m2e/documentation/m2e-execution-not-covered.html). | ||
|
||
## Workaround | ||
- Option 1: Use [build-helper-maven-plugin](http://www.mojohaus.org/build-helper-maven-plugin/usage.html) to explicitly add the unrecoginzed source folder to classpath. | ||
|
||
```xml | ||
<project> | ||
... | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>build-helper-maven-plugin</artifactId> | ||
<version>3.2.0</version> | ||
<executions> | ||
<execution> | ||
<id>add-source</id> | ||
<phase>generate-sources</phase> | ||
<goals> | ||
<goal>add-source</goal> | ||
</goals> | ||
<configuration> | ||
<sources> | ||
<source>some directory</source> | ||
... | ||
</sources> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> | ||
``` | ||
|
||
- Option 2: Configure a lifecycle mapping metadata in pom.xml that explicitly tells m2e what to do with your plugin. | ||
|
||
For example, add [a processing instruction in the pom.xml](https://www.eclipse.org/m2e/documentation/release-notes-17.html#new-syntax-for-specifying-lifecycle-mapping-metadata) like `<?m2e execute onConfiguration?>` to execute it on every project configuration udpate. | ||
|
||
You can use quick fixes to generate the inline lifecycle mapping in pom.xml, or manually configure it in pom.xml. If it's yourself that manually configure it, you have to let VS Code update the project configuration as well. The command is `"Java: Update Project"`. | ||
|
||
```xml | ||
<project> | ||
... | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>ro.isdc.wro4j</groupId> | ||
<artifactId>wro4j-maven-plugin</artifactId> | ||
<version>1.8.0</version> | ||
<executions> | ||
<execution> | ||
<?m2e execute onConfiguration?> | ||
<phase>generate-resources</phase> | ||
<goals> | ||
<goal>run</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> | ||
``` | ||
|
||
If you have multiple Maven plugins that need to configure the lifecycle mapping metadata, you can also configure them together in a dedicated `pluginManagement` section. | ||
|
||
```xml | ||
<project> | ||
... | ||
<build> | ||
<pluginManagement> | ||
<plugins> | ||
<!-- This plugin's configuration is used to store m2e settings only. | ||
It has no influence on the Maven build itself. --> | ||
<plugin> | ||
<groupId>org.eclipse.m2e</groupId> | ||
<artifactId>lifecycle-mapping</artifactId> | ||
<version>1.0.0</version> | ||
<configuration> | ||
<lifecycleMappingMetadata> | ||
<pluginExecutions> | ||
<pluginExecution> | ||
<pluginExecutionFilter> | ||
<groupId>ro.isdc.wro4j</groupId> | ||
<artifactId>wro4j-maven-plugin</artifactId> | ||
<versionRange>[1.8.0,)</versionRange> | ||
<goals> | ||
<goal>run</goal> | ||
</goals> | ||
</pluginExecutionFilter> | ||
<action> | ||
<execute> | ||
<runOnConfiguration>true</runOnConfiguration> | ||
</execute> | ||
</action> | ||
</pluginExecution> | ||
</pluginExecutions> | ||
</lifecycleMappingMetadata> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
</build> | ||
</project> | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
'use strict'; | ||
|
||
import { CodeActionProvider, CodeAction, TextDocument, Range, Selection, CodeActionContext, CancellationToken, ProviderResult, Command, CodeActionKind, Diagnostic, WorkspaceEdit, Position, EndOfLine, ExtensionContext, commands, CodeActionProviderMetadata, workspace, Uri } from "vscode"; | ||
import { Commands } from "../commands"; | ||
|
||
export class PomCodeActionProvider implements CodeActionProvider<CodeAction> { | ||
constructor(context: ExtensionContext) { | ||
context.subscriptions.push(commands.registerCommand("_java.projectConfiguration.saveAndUpdate", async (uri: Uri) => { | ||
const document: TextDocument = await workspace.openTextDocument(uri); | ||
await document.save(); | ||
commands.executeCommand(Commands.CONFIGURATION_UPDATE, uri); | ||
})); | ||
} | ||
|
||
provideCodeActions(document: TextDocument, range: Range | Selection, context: CodeActionContext, token: CancellationToken): ProviderResult<(Command | CodeAction)[]> { | ||
if (context?.diagnostics?.length && context.diagnostics[0].source === "Java") { | ||
return this.collectCodeActionsForNotCoveredExecutions(document, context.diagnostics); | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
private collectCodeActionsForNotCoveredExecutions(document: TextDocument, diagnostics: readonly Diagnostic[]): CodeAction[] { | ||
const codeActions: CodeAction[] = []; | ||
for (const diagnostic of diagnostics) { | ||
if (diagnostic.message?.startsWith("Plugin execution not covered by lifecycle configuration")) { | ||
const indentation = this.getNewTextIndentation(document, diagnostic); | ||
const saveAndUpdateConfigCommand: Command = { | ||
title: "Save and update project configuration", | ||
command: "_java.projectConfiguration.saveAndUpdate", | ||
arguments: [ document.uri ], | ||
}; | ||
|
||
const action1 = new CodeAction("Enable this execution in project configuration phase", CodeActionKind.QuickFix.append("pom")); | ||
action1.edit = new WorkspaceEdit(); | ||
action1.edit.insert(document.uri, new Position(diagnostic.range.end.line, diagnostic.range.end.character + 1), | ||
indentation + "<?m2e execute onConfiguration?>"); | ||
action1.command = saveAndUpdateConfigCommand; | ||
codeActions.push(action1); | ||
|
||
const action2 = new CodeAction("Enable this execution in project build phase", CodeActionKind.QuickFix.append("pom")); | ||
action2.edit = new WorkspaceEdit(); | ||
action2.edit.insert(document.uri, new Position(diagnostic.range.end.line, diagnostic.range.end.character + 1), | ||
indentation + "<?m2e execute onConfiguration,onIncremental?>"); | ||
action2.command = saveAndUpdateConfigCommand; | ||
codeActions.push(action2); | ||
|
||
const action3 = new CodeAction("Mark this execution as ignored in pom.xml", CodeActionKind.QuickFix.append("pom")); | ||
action3.edit = new WorkspaceEdit(); | ||
action3.edit.insert(document.uri, new Position(diagnostic.range.end.line, diagnostic.range.end.character + 1), | ||
indentation + "<?m2e ignore?>"); | ||
action3.command = saveAndUpdateConfigCommand; | ||
codeActions.push(action3); | ||
} | ||
} | ||
|
||
return codeActions; | ||
} | ||
|
||
private getNewTextIndentation(document: TextDocument, diagnostic: Diagnostic): string { | ||
const textline = document.lineAt(diagnostic.range.end.line); | ||
if (textline.text.lastIndexOf("</execution>") > diagnostic.range.end.character) { | ||
return ""; | ||
} | ||
|
||
const lineSeparator = document.eol === EndOfLine.LF ? "\r" : "\r\n"; | ||
const currentTextIndentation = textline.text.substring(0, textline.firstNonWhitespaceCharacterIndex); | ||
const tabSize = currentTextIndentation.length / 5; | ||
const insertChar = currentTextIndentation && currentTextIndentation.charAt(0) === ' ' ? ' ' : ' '; | ||
let newIndentation = lineSeparator + currentTextIndentation; | ||
for (let i = 0; i < tabSize; i++) { | ||
newIndentation += insertChar; | ||
} | ||
|
||
return newIndentation; | ||
} | ||
} | ||
|
||
export const pomCodeActionMetadata: CodeActionProviderMetadata = { | ||
providedCodeActionKinds: [ | ||
CodeActionKind.QuickFix.append("pom") | ||
], | ||
documentation: [ | ||
{ | ||
kind: CodeActionKind.QuickFix.append("pom"), | ||
command: { | ||
title: "Learn more about not covered Maven execution", | ||
command: Commands.NOT_COVERED_EXECUTION | ||
} | ||
} | ||
], | ||
}; |
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