-
Notifications
You must be signed in to change notification settings - Fork 7
/
OverlayConfigurator.java
213 lines (183 loc) · 8.68 KB
/
OverlayConfigurator.java
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*******************************************************************************
* Copyright (c) 2008-2014 Sonatype, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.m2e.wtp;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.war.Overlay;
import org.apache.maven.project.MavenProject;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jst.j2ee.web.project.facet.WebFacetUtils;
import org.eclipse.m2e.core.lifecyclemapping.model.IPluginExecutionMetadata;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.MavenProjectChangedEvent;
import org.eclipse.m2e.core.project.configurator.AbstractBuildParticipant;
import org.eclipse.m2e.core.project.configurator.ILifecycleMappingConfiguration;
import org.eclipse.m2e.core.project.configurator.MojoExecutionKey;
import org.eclipse.m2e.core.project.configurator.ProjectConfigurationRequest;
import org.eclipse.m2e.jdt.IClasspathDescriptor;
import org.eclipse.m2e.wtp.internal.StringUtils;
import org.eclipse.m2e.wtp.overlay.modulecore.IOverlayVirtualComponent;
import org.eclipse.m2e.wtp.overlay.modulecore.OverlayComponentCore;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.common.componentcore.resources.IVirtualReference;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
/**
* Configurator for war overlays.
*
* @provisional This class has been added as part of a work in progress. It is
* not guaranteed to work or remain the same in future releases. For more
* information contact
* <a href="mailto:m2e-wtp-dev@eclipse.org">m2e-wtp-dev@eclipse.org</a>.
*
* @author Fred Bricon
*/
public class OverlayConfigurator extends WTPProjectConfigurator {
@Override
public void configure(ProjectConfigurationRequest request, IProgressMonitor monitor) throws CoreException {
mavenProjectChanged(request.mavenProjectFacade(), monitor);
}
@Override
public void mavenProjectChanged(MavenProjectChangedEvent event, IProgressMonitor monitor) throws CoreException {
mavenProjectChanged(event.getMavenProject(), monitor);
}
private void mavenProjectChanged(IMavenProjectFacade facade, IProgressMonitor monitor) throws CoreException {
if (facade == null) {
return;
}
IProject project = facade.getProject();
if (!project.isAccessible() || WTPProjectsUtil.isM2eWtpDisabled(facade, monitor)
|| project.getResourceAttributes() == null || project.getResourceAttributes().isReadOnly()) {
return;
}
IFacetedProject facetedProject = ProjectFacetsManager.create(project, true, monitor);
if (!facetedProject.hasProjectFacet(WebFacetUtils.WEB_FACET)) {
return;
}
try {
markerManager.deleteMarkers(facade.getPom(), MavenWtpConstants.WTP_MARKER_OVERLAY_ERROR);
setModuleDependencies(facade, monitor);
} catch (Exception ex) {
markerManager.addErrorMarkers(facade.getPom(), MavenWtpConstants.WTP_MARKER_OVERLAY_ERROR, ex);
}
}
/**
* @param project
* @param mavenProject
* @param monitor
* @throws CoreException
*/
private void setModuleDependencies(IMavenProjectFacade facade, IProgressMonitor monitor) throws CoreException {
IProject project = facade.getProject();
IVirtualComponent warComponent = ComponentCore.createComponent(project);
if (warComponent == null) {
return;
}
Set<IVirtualReference> newOverlayRefs = new LinkedHashSet<>();
MavenSessionHelper helper = new MavenSessionHelper(facade);
try {
helper.ensureDependenciesAreResolved("maven-war-plugin", "war:war", monitor); //$NON-NLS-1$ //$NON-NLS-2$
MavenProject mavenProject = helper.getMavenProject();
WarPluginConfiguration config = new WarPluginConfiguration(mavenProject, project);
List<Overlay> overlays = config.getOverlays();
// 1 overlay = current project => no overlay component needed
if (overlays.size() > 1) {
// Component order must be inverted to follow maven's overlay
// order behaviour
// as in WTP, last components supersede the previous ones
Collections.reverse(overlays);
for (Overlay overlay : overlays) {
if (overlay.shouldSkip()) {
continue;
}
Artifact artifact = overlay.getArtifact();
IOverlayVirtualComponent overlayComponent = null;
IMavenProjectFacade workspaceDependency = projectManager.getMavenProject(artifact.getGroupId(),
artifact.getArtifactId(), artifact.getVersion());
if (workspaceDependency != null) {
// artifact dependency is a workspace project &&
// dependency resolution is on
IProject overlayProject = workspaceDependency.getProject();
if (overlayProject.equals(project)) {
overlayComponent = OverlayComponentCore.createSelfOverlayComponent(project);
} else if (workspaceDependency.getFullPath(artifact.getFile()) != null) {
WTPProjectsUtil.configureWtpUtil(workspaceDependency, monitor);
overlayComponent = OverlayComponentCore.createOverlayComponent(overlayProject);
} else {
// Dependency resolution is off
overlayComponent = createOverlayArchiveComponent(project, mavenProject, overlay);
}
} else {
overlayComponent = createOverlayArchiveComponent(project, mavenProject, overlay);
}
if (overlayComponent != null) {
overlayComponent.setInclusions(new LinkedHashSet<>(Arrays.asList(overlay.getIncludes())));
overlayComponent.setExclusions(new LinkedHashSet<>(Arrays.asList(overlay.getExcludes())));
IVirtualReference depRef = ComponentCore.createReference(warComponent, overlayComponent);
String targetPath = StringUtils.nullOrEmpty(overlay.getTargetPath()) ? "/" //$NON-NLS-1$
: overlay.getTargetPath();
depRef.setRuntimePath(new Path(targetPath));
newOverlayRefs.add(depRef);
}
}
}
IVirtualReference[] oldOverlayRefs = WTPProjectsUtil.extractHardReferences(warComponent, true);
IVirtualReference[] updatedOverlayRefs = newOverlayRefs
.toArray(new IVirtualReference[newOverlayRefs.size()]);
if (WTPProjectsUtil.hasChanged2(oldOverlayRefs, updatedOverlayRefs)) {
// Only write in the .component file if necessary
IVirtualReference[] nonOverlayRefs = WTPProjectsUtil.extractHardReferences(warComponent, false);
IVirtualReference[] allRefs = new IVirtualReference[nonOverlayRefs.length + updatedOverlayRefs.length];
System.arraycopy(nonOverlayRefs, 0, allRefs, 0, nonOverlayRefs.length);
System.arraycopy(updatedOverlayRefs, 0, allRefs, nonOverlayRefs.length, updatedOverlayRefs.length);
warComponent.setReferences(allRefs);
}
} finally {
helper.dispose();
}
}
private IOverlayVirtualComponent createOverlayArchiveComponent(IProject project, MavenProject mavenProject,
Overlay overlay) throws CoreException {
IPath m2eWtpFolder = ProjectUtils.getM2eclipseWtpFolder(mavenProject, project);
IPath unpackDirPath = new Path(m2eWtpFolder.toOSString() + "/overlays"); //$NON-NLS-1$
String archiveLocation = ArtifactHelper.getM2REPOVarPath(overlay.getArtifact());
String targetPath = StringUtils.nullOrEmpty(overlay.getTargetPath()) ? "/" : overlay.getTargetPath(); //$NON-NLS-1$
IOverlayVirtualComponent component = OverlayComponentCore.createOverlayArchiveComponent(project,
archiveLocation, unpackDirPath, new Path(targetPath));
return component;
}
@Override
public void configureClasspath(IMavenProjectFacade facade, IClasspathDescriptor classpath, IProgressMonitor monitor)
throws CoreException {
}
@Override
public AbstractBuildParticipant getBuildParticipant(IMavenProjectFacade projectFacade, MojoExecution execution,
IPluginExecutionMetadata executionMetadata) {
return null;
}
@Override
public boolean hasConfigurationChanged(IMavenProjectFacade newFacade,
ILifecycleMappingConfiguration oldProjectConfiguration, MojoExecutionKey key, IProgressMonitor monitor) {
// Changes to maven-war-plugin in pom.xml don't make it "dirty"
// wrt Overlay configuration (i.e. no need to invoke configure(request,
// monitor))
return false;
}
}