Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f5b730ea77fb20e73ef856d6825f5b1c29f172ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*******************************************************************************
 * Copyright (c) 2008-2011 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.core.ui.internal.editing;

import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.eclipse.m2e.core.internal.lifecyclemapping.LifecycleMappingFactory;
import org.eclipse.m2e.core.lifecyclemapping.model.PluginExecutionAction;

public class LifecycleMappingOperation implements Operation {
  

  private static final Logger log = LoggerFactory.getLogger(LifecycleMappingOperation.class);

  private static final String LIFECYCLE_PLUGIN_VERSION = LifecycleMappingFactory.LIFECYCLE_MAPPING_PLUGIN_VERSION;

  private static final String LIFECYCLE_PLUGIN_ARTIFACTID = LifecycleMappingFactory.LIFECYCLE_MAPPING_PLUGIN_ARTIFACTID;

  private static final String LIFECYCLE_PLUGIN_GROUPID = LifecycleMappingFactory.LIFECYCLE_MAPPING_PLUGIN_GROUPID;

  private String version;
  private String groupId;
  private String artifactId;

  private PluginExecutionAction action;
  private String[] goals;

  public LifecycleMappingOperation(String pluginGroupId, String pluginArtifactId, String pluginVersion,
      PluginExecutionAction action, String[] goals) {
    this.artifactId = pluginArtifactId;
    this.groupId = pluginGroupId;
    this.version = pluginVersion;
    assert !PluginExecutionAction.configurator.equals(action);
    this.action = action;
    this.goals = goals;
  }

  public void process(Document document) {
    Element root = document.getDocumentElement();
    Element managedPlugins = getChild(root, BUILD, PLUGIN_MANAGEMENT, PLUGINS);
    //now find the lifecycle stuff if it's there.
    Element lifecyclePlugin = findChild(managedPlugins, PLUGIN, 
        childEquals(GROUP_ID, LIFECYCLE_PLUGIN_GROUPID), 
        childEquals(ARTIFACT_ID, LIFECYCLE_PLUGIN_ARTIFACTID));
    if (lifecyclePlugin == null) {
      //not found, create
      lifecyclePlugin = PomHelper.createPlugin(managedPlugins, LIFECYCLE_PLUGIN_GROUPID, LIFECYCLE_PLUGIN_ARTIFACTID, LIFECYCLE_PLUGIN_VERSION);

      //mkleint: a bit scared to have this text localized, with chinese/japanese locales, it could write garbage into the pom file..
      Comment comment = document.createComment("This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.");
      managedPlugins.insertBefore(comment, lifecyclePlugin);
      format(comment);
    }
    
    Element pluginExecutions = getChild(lifecyclePlugin, CONFIGURATION, "lifecycleMappingMetadata", "pluginExecutions"); //$NON-NLS-1$ //$NON-NLS-2$
    //now find the plugin execution for the plugin we have..
    Element execution = null;
    for (Element exec : findChilds(pluginExecutions, "pluginExecution")) { //$NON-NLS-1$
      Element filter = findChild(exec, "pluginExecutionFilter",  //$NON-NLS-1$
          childEquals(GROUP_ID, groupId), 
          childEquals(ARTIFACT_ID, artifactId));
      //the action needs to match the action we want..
      Element actionEl = findChild(findChild(exec, "action"), action.toString()); //$NON-NLS-1$
      if (filter != null && actionEl != null) {
        String versionRange = getTextValue(getChild(filter, "versionRange")); //$NON-NLS-1$
        if (versionRange != null) { //  paranoid null check
          //now we shall do some smart matching on the existing versionRange and our version..
          //so far the "smart" thing involves just overwriting the range.
          try {
            VersionRange range = VersionRange.createFromVersionSpec(versionRange);
            if (!range.containsVersion(new DefaultArtifactVersion(version))) {
              Element rangeEl = findChild(filter, "versionRange"); //$NON-NLS-1$
              setText(rangeEl, "[" + version + ",)");
            }
          } catch(InvalidVersionSpecificationException e) {
            log.error("Failed to parse version range:" + versionRange, e); //$NON-NLS-1$
          }
        }
        execution = exec;
        break;
      }
    }
    if (execution == null) {
      execution = createPluginExecution(document, pluginExecutions);
    }
    //now enter/update the goal(s)..
    Element goalsEl = getChild(execution, "pluginExecutionFilter", GOALS); //$NON-NLS-1$
    List<String> toAddGoals = new ArrayList<String>(Arrays.asList(goals));
    for (Element existingGoal : findChilds(goalsEl, GOAL)) {
      String glValue = getTextValue(existingGoal);
      if (glValue != null && toAddGoals.contains(glValue)) {
        toAddGoals.remove(glValue);
      }
    }
    if (toAddGoals.size() > 0) {
      for (String goal : toAddGoals) {
        format(createElementWithText(goalsEl, GOAL, goal));
      }
    }
    
  }

  private Element createPluginExecution(Document document, Element parent) {
    Element exec = document.createElement("pluginExecution"); //$NON-NLS-1$
    parent.appendChild(exec);
    Element filter = document.createElement("pluginExecutionFilter"); //$NON-NLS-1$
    exec.appendChild(filter);
    createElementWithText(filter, GROUP_ID, groupId);
    createElementWithText(filter, ARTIFACT_ID, artifactId);
    createElementWithText(filter, "versionRange", "[" + version + ",)"); //$NON-NLS-1$
    
    Element actionEl = document.createElement("action"); //$NON-NLS-1$
    exec.appendChild(actionEl);
    Element actionEl2 = document.createElement(action.toString());
    actionEl.appendChild(actionEl2);
    if(PluginExecutionAction.execute.equals(action)) {
      //mkleint: a bit scared to have this text localized, with chinese/japanese locales, it could write garbage into the pom file..
      actionEl2.appendChild(document.createComment("use <runOnIncremental>false</runOnIncremental>to only execute the mojo during full/clean build"));
    }
    
    format(exec);
    return exec;
  }

}

Back to the top