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

Work better with custom exec-style goals #6805

Merged
merged 2 commits into from
Dec 12, 2023
Merged
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 @@ -67,6 +67,17 @@
</folder>
</folder>
<folder name="org-netbeans-modules-maven">
<folder name="RunGoals">
<file name="mn">
<attr name="alias" stringvalue="./io.micronaut.maven:micronaut-maven-plugin"/>
</file>
<file name="io.micronaut.maven:micronaut-maven-plugin">
<attr name="goals" stringvalue="run"/>
</file>
<file name="io.micronaut.build:micronaut-maven-plugin">
<attr name="goals" stringvalue="run"/>
</file>
</folder>
<!-- For Micornaut 3.x -->
<folder name="io.micronaut.build:micronaut-maven-plugin">
<folder name="Lookup">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
<exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args>
<exec.appArgs></exec.appArgs>
<exec.mainClass>${packageClassName}</exec.mainClass>
<mn.appArgs>${exec.appArgs}</mn.appArgs>
<mn.jvmArgs>${exec.vmArgs}</mn.jvmArgs>
</properties>
</action>
<action>
Expand All @@ -69,6 +71,8 @@
<exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args>
<exec.mainClass>${packageClassName}</exec.mainClass>
<exec.classpathScope>${classPathScope}</exec.classpathScope>
<mn.appArgs>${exec.appArgs}</mn.appArgs>
<mn.jvmArgs>${exec.vmArgs}</mn.jvmArgs>
</properties>
</action>

Expand Down
19 changes: 19 additions & 0 deletions java/maven/apichanges.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ is the proper place.
<!-- ACTUAL CHANGES BEGIN HERE: -->

<changes>
<change id="declare-run-goals">
<api name="general"/>
<summary>Allow extensions to declare run-like goals</summary>
<version major="2" minor="161"/>
<date day="6" month="12" year="2023"/>
<author login="sdedic"/>
<compatibility addition="yes" semantic="compatible"/>
<description>
<p>
The GUI recognizes user modifications to actions and refuses to configure
a modified "run" action with vm and app arguments. It is now possible for a plugin
to register an alternative goal, which is recognized as "exec"-like one and does
not disable the GUI.
</p>
<p>
For an example, see <a href="@TOP@/org/netbeans/modules/maven/spi/actions/MavenActionsProvider.html#declare-run-goals">MavenActionProvider</a>.
</p>
</description>
</change>
<change id="runutils-createconfig-action">
<api name="general"/>
<summary></summary>
Expand Down
5 changes: 5 additions & 0 deletions java/maven/arch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@
to control (usually to disable with <code>DEFAULT_UPDATE_FREQ=3</code>)
the default behavior automatic maven index downloading.
</api>
<api category="devel" group="layer" name="Project.org-netbeans-modules-maven.RunGoals" type="export">
A executable-like plugin goals, that may serve in place of exec:exec may be registered here. The core implementation
will recognize such goals as exec's and will not disable app amd VM parameter configuration UI.
For an example, see <a href="@TOP@/org/netbeans/modules/maven/spi/actions/MavenActionsProvider.html#declare-run-goals">MavenActionProvider</a>.
</api>
</p>

</answer>
Expand Down
2 changes: 1 addition & 1 deletion java/maven/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ javadoc.apichanges=${basedir}/apichanges.xml
javadoc.arch=${basedir}/arch.xml
javahelp.hs=maven.hs
extra.module.files=maven-nblib/
spec.version.base=2.160.0
spec.version.base=2.161.0

# The CPExtender test fails in library processing (not randomly) since NetBeans 8.2; disabling.
test.excludes=**/CPExtenderTest.class
Expand Down
3 changes: 3 additions & 0 deletions java/maven/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,9 @@
<compile-dependency/>
<test/>
</test-dependency>
<test-dependency>
<code-name-base>org.netbeans.api.maven</code-name-base>
</test-dependency>
</test-type>
</test-dependencies>
<friend-packages>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.netbeans.modules.maven.api.customizer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -272,13 +274,21 @@ public static Configuration createProfileConfiguration(String id) {
Configuration conf = new Configuration();
conf.setId(id);
conf.setProfileBased(true);
List<String> l = new ArrayList<>();
if (!id.equals(M2Configuration.DEFAULT)) {
l.add(id);
}
conf.setActivatedProfiles(Collections.singletonList(id));
conf.setProperties(new HashMap<>());
return conf;
}

public static Configuration createDefaultConfiguration() {
Configuration conf = new Configuration();
conf.setId(M2Configuration.DEFAULT);
conf.setDefault(true);
conf.setActivatedProfiles(new ArrayList<>());
conf.setProperties(new HashMap<>());
return conf;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
Expand All @@ -51,11 +52,14 @@
import org.netbeans.modules.maven.NbMavenProjectImpl;
import org.netbeans.modules.maven.api.customizer.ModelHandle2;
import org.netbeans.modules.maven.classpath.MavenSourcesImpl;
import org.netbeans.modules.maven.configurations.M2ConfigProvider;
import org.netbeans.modules.maven.configurations.M2Configuration;
import org.netbeans.modules.maven.runjar.MavenExecuteUtils;
import org.netbeans.modules.maven.execute.model.ActionToGoalMapping;
import org.netbeans.modules.maven.execute.model.NetbeansActionMapping;
import org.netbeans.modules.maven.options.MavenSettings;
import org.netbeans.spi.project.ActionProvider;
import org.netbeans.spi.project.ProjectConfiguration;
import org.netbeans.spi.project.ui.support.ProjectCustomizer;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
Expand Down Expand Up @@ -174,11 +178,23 @@ private void removeListeners() {

private MavenExecuteUtils.ExecutionEnvHelper execEnvHelper;

private NetbeansActionMapping getMapping(String action, ProjectConfiguration c) {
NetbeansActionMapping r = null;

if (c != null) {
r = ModelHandle2.getMapping(action, project, c);
} else {
r = ModelHandle2.getDefaultMapping(action, project);
}
return r;
}

@NbBundle.Messages({"MsgModifiedAction=One of Run/Debug/Profile Project actions has been modified and the Run panel cannot be safely edited"})
private void initValues() {
run = null;
debug = null;
profile = null;
ModelHandle2.Configuration sel = (ModelHandle2.Configuration) comConfiguration.getSelectedItem();
ActionToGoalMapping mapp = handle.getActionMappings((ModelHandle2.Configuration) comConfiguration.getSelectedItem());
@SuppressWarnings("unchecked")
List<NetbeansActionMapping> lst = mapp.getActions();
Expand All @@ -193,14 +209,22 @@ private void initValues() {
profile = m;
}
}
M2ConfigProvider prov = project.getLookup().lookup(M2ConfigProvider.class);
M2Configuration config = null;
if (prov != null) {
config = prov.getConfigurations().stream().filter(c -> c.getId().equals(sel.getId())).findAny().orElse(null);
if (M2Configuration.DEFAULT.equals(config.getId())) {
config = null;
}
}
if (run == null) {
run = ModelHandle2.getDefaultMapping(ActionProvider.COMMAND_RUN, project);
run = getMapping(ActionProvider.COMMAND_RUN, config);
}
if (debug == null) {
debug = ModelHandle2.getDefaultMapping(ActionProvider.COMMAND_DEBUG, project);
debug = getMapping(ActionProvider.COMMAND_DEBUG, config);
}
if (profile == null) {
profile = ModelHandle2.getDefaultMapping(PROFILE_CMD, project);
profile = getMapping(PROFILE_CMD, config);
}
execEnvHelper = MavenExecuteUtils.createExecutionEnvHelper(project, run, debug, profile, mapp);
execEnvHelper.loadFromProject();
Expand Down
8 changes: 8 additions & 0 deletions java/maven/src/org/netbeans/modules/maven/layer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@
</folder>
</folder>
<file name="nbactions.xml" url="nbactions.xml"/>
<folder name="RunGoals">
<file name="exec">
<attr name="alias" stringvalue="org.codehaus.mojo:exec-maven-plugin"/>
</file>
<file name="org.codehaus.mojo:exec-maven-plugin">
<attr name="goals" stringvalue="exec"/>
</file>
</folder>
</folder>
</folder>
<folder name="OptionsExport">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.netbeans.modules.maven.runjar;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
Expand All @@ -34,6 +36,9 @@
import org.netbeans.modules.maven.execute.model.ActionToGoalMapping;
import org.netbeans.modules.maven.execute.model.NetbeansActionMapping;
import org.netbeans.spi.project.ActionProvider;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.URLMapper;

/**
*
Expand Down Expand Up @@ -92,6 +97,11 @@ public final class MavenExecuteUtils {
*/
public static final String PROFILE_CMD = "profile"; // NOI18N

/**
* Folder on config filesystem that contains "run" goal aliases.
*/
private static final String RUN_GOALS_CONFIG_ROOT = "Projects/org-netbeans-modules-maven/RunGoals";

/**
* A helper that can update action mappings based on changes
* made on the helper instance. Use {@link #createExecutionEnvHelper}
Expand Down Expand Up @@ -321,11 +331,47 @@ private boolean checkNewMapping(NetbeansActionMapping map) {
if (map == null || map.getGoals() == null) {
return false; //#164323
}
if (map.getGoals().isEmpty()) {
return true;
}
Iterator it = map.getGoals().iterator();
FileObject goalRoot = FileUtil.getConfigFile(RUN_GOALS_CONFIG_ROOT); // NOI18N
while (it.hasNext()) {
String goal = (String) it.next();
if (goal.matches("org\\.codehaus\\.mojo\\:exec-maven-plugin\\:(.)+\\:exec") //NOI18N
|| goal.indexOf("exec:exec") > -1) { //NOI18N
boolean goalFound = (goal.matches("org\\.codehaus\\.mojo\\:exec-maven-plugin\\:(.)+\\:exec") //NOI18N
|| goal.contains("exec:exec")); // NOI18N
if (!goalFound && goalRoot != null) {
int colon = goal.lastIndexOf(':');
if (colon != -1) {
String pluginId = goal.substring(0, colon);
String goalName = goal.substring(colon + 1);
String[] gav = pluginId.split(":");

FileObject g = goalRoot.getFileObject(pluginId);
if (g == null && gav.length > 2) {
String justId = gav[0] + ":" + gav[1];
g = goalRoot.getFileObject(justId);
}
if (g != null) {
Object alias = g.getAttribute("alias");
if (alias instanceof String) {
try {
URL u = new URL(URLMapper.findURL(g, URLMapper.INTERNAL), alias.toString());
g = URLMapper.findFileObject(u);
} catch (MalformedURLException ex) {
// expected
}
}
}
if (g != null) {
Object s = g.getAttribute("goals");
if (s instanceof String) {
goalFound = Arrays.asList(s.toString().split(" ")).contains(goalName);
}
}
}
}
if (goalFound) { //NOI18N
if (map.getProperties() != null) {
if (map.getProperties().containsKey("exec.args")) {
String execArgs = map.getProperties().get("exec.args");
Expand Down Expand Up @@ -363,6 +409,10 @@ public void applyToMappings() {

private void updateAction(NetbeansActionMapping mapping, String debuVMArgs) {
boolean changed = false;
// do not update for actiosn that have empty goals = are disabled.
if (mapping.getGoals() == null || mapping.getGoals().isEmpty()) {
return;
}

if (!oldWorkDir.equals(workDir)) {
mapping.addProperty(RUN_WORKDIR, workDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@
* <b>Since 2.149</b> the returned {@link NetbeansActionMapping} can be disabled - checked by
* {@link ActionToGoalUtils#isDisabledMapping}. Such mapping will override the action that may be even enabled by a farther
* {@link MavenActionsProvider}. The {@link ActionProvider} exported from the project will report such action as disabled.
*
* <p>
* <span id="declare-run-goals">
* <b>Since 1.161</b>, specific plugin goals can be declaratively marked as 'run' goals; if they are used in actions, NetBeans maven core
* recognizes them and allows to configure vm, application arguments and main class name for the goal. It is a responsibility
* of action mapping to remap the properties used by exec:exec to goal-specific properties, if it uses different naming. See the
* following example:
* <div class="nonnormative">
* {@snippet file="org/netbeans/modules/maven/runjar/example-rungoals-config.xml" region="register-run-goals"}
* </div>
* </span>
* @author Milos Kleint
*/
public interface MavenActionsProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -132,13 +133,17 @@ protected String substProperties(String input, String token, Map<String, String>
return input;
}

protected InputStream getActionResourceStream() {
return getClass().getResourceAsStream("nbactions-template.xml");
}

protected FileObject createNbActions(
Map<String, String> runProperties,
Map<String, String> debugProperties,
Map<String, String> profileProperties) throws IOException, XmlPullParserException {
StringBuilder sb = new StringBuilder();
try (BufferedReader rdr = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("nbactions-template.xml"), StandardCharsets.UTF_8))) {
getActionResourceStream(), StandardCharsets.UTF_8))) {
String l;

while ((l = rdr.readLine()) != null) {
Expand Down Expand Up @@ -281,7 +286,7 @@ protected List<String> substituteProperties(List<String> args, Map<String, Strin
protected void assertRunArguments(NetbeansActionMapping mapping, String vmArgs, String mainClass, String args) throws Exception {
final Project project = ProjectManager.getDefault().findProject(pom.getParent());

assertMavenRunAction(project, mapping, "run", (List<String> cmdLine) -> {
assertMavenRunAction(project, mapping, "run", mainClass, (List<String> cmdLine) -> {
String argString = mavenExecutorDefines.get(MavenExecuteUtils.RUN_PARAMS);
int indexOfMainClass = argString.indexOf(mainClass);
assertTrue(indexOfMainClass >= 0);
Expand Down Expand Up @@ -312,10 +317,14 @@ protected void assertRunArguments(NetbeansActionMapping mapping, String vmArgs,
}

protected void assertMavenRunAction(Project project, NetbeansActionMapping mapping, String actionName, Consumer<List<String>> commandLineAcceptor) throws Exception {
assertMavenRunAction(project, mapping, actionName, null, commandLineAcceptor);
}

protected void assertMavenRunAction(Project project, NetbeansActionMapping mapping, String actionName, String mainClassname, Consumer<List<String>> commandLineAcceptor) throws Exception {
NbPreferences.root().node("org/netbeans/modules/maven").put(EmbedderFactory.PROP_COMMANDLINE_PATH, "mvn");
ModelRunConfig cfg = new ModelRunConfig(project, mapping, actionName, null, actionLookup, true);
// prevent displaying dialogs.
RunJarPrereqChecker.setMainClass(DEFAULT_MAIN_CLASS_TOKEN);
RunJarPrereqChecker.setMainClass(mainClassname == null ? DEFAULT_MAIN_CLASS_TOKEN : mainClassname);
for (PrerequisitesChecker elem : cfg.getProject().getLookup().lookupAll(PrerequisitesChecker.class)) {
if (!elem.checkRunConfig(cfg)) {
fail("");
Expand Down
Loading
Loading