| 1 | |
package org.apache.maven.plugin.war.overlay; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
import org.apache.maven.artifact.Artifact; |
| 23 | |
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; |
| 24 | |
import org.apache.maven.plugin.war.Overlay; |
| 25 | |
import org.apache.maven.project.MavenProject; |
| 26 | |
import org.codehaus.plexus.util.StringUtils; |
| 27 | |
|
| 28 | |
import java.util.ArrayList; |
| 29 | |
import java.util.Arrays; |
| 30 | |
import java.util.Iterator; |
| 31 | |
import java.util.List; |
| 32 | |
import java.util.ListIterator; |
| 33 | |
import java.util.Set; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
public class OverlayManager |
| 43 | |
{ |
| 44 | |
private final List overlays; |
| 45 | |
|
| 46 | |
private final MavenProject project; |
| 47 | |
|
| 48 | |
private final List artifactsOverlays; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public OverlayManager( List overlays, MavenProject project, String defaultIncludes, String defaultExcludes, |
| 65 | |
Overlay currentProjectOverlay ) |
| 66 | |
throws InvalidOverlayConfigurationException |
| 67 | 66 | { |
| 68 | 66 | this.overlays = new ArrayList(); |
| 69 | 66 | if ( overlays != null ) |
| 70 | |
{ |
| 71 | 66 | this.overlays.addAll( overlays ); |
| 72 | |
} |
| 73 | 66 | this.project = project; |
| 74 | |
|
| 75 | 66 | this.artifactsOverlays = getOverlaysAsArtifacts(); |
| 76 | |
|
| 77 | |
|
| 78 | 66 | initialize( defaultIncludes, defaultExcludes, currentProjectOverlay ); |
| 79 | |
|
| 80 | 65 | } |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public List getOverlays() |
| 89 | |
{ |
| 90 | 78 | return overlays; |
| 91 | |
} |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
public List getOverlayIds() |
| 99 | |
{ |
| 100 | 61 | final Iterator it = overlays.iterator(); |
| 101 | 61 | final List result = new ArrayList(); |
| 102 | 156 | while ( it.hasNext() ) |
| 103 | |
{ |
| 104 | 95 | Overlay overlay = (Overlay) it.next(); |
| 105 | 95 | result.add( overlay.getId() ); |
| 106 | 95 | } |
| 107 | 61 | return result; |
| 108 | |
|
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
void initialize( String defaultIncludes, String defaultExcludes, Overlay currentProjectOverlay ) |
| 121 | |
throws InvalidOverlayConfigurationException |
| 122 | |
{ |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | 66 | final List configuredWarArtifacts = new ArrayList(); |
| 127 | 66 | final ListIterator it = overlays.listIterator(); |
| 128 | 98 | while ( it.hasNext() ) |
| 129 | |
{ |
| 130 | 33 | Overlay overlay = (Overlay) it.next(); |
| 131 | 33 | if ( overlay == null ) |
| 132 | |
{ |
| 133 | 0 | throw new InvalidOverlayConfigurationException( "overlay could not be null." ); |
| 134 | |
} |
| 135 | |
|
| 136 | 33 | if ( overlay.isCurrentProject() ) |
| 137 | |
{ |
| 138 | 4 | overlay = currentProjectOverlay; |
| 139 | 4 | it.set( overlay ); |
| 140 | |
} |
| 141 | |
|
| 142 | 33 | if ( Arrays.equals( Overlay.DEFAULT_INCLUDES, overlay.getIncludes() ) |
| 143 | |
&& Arrays.equals( Overlay.DEFAULT_EXCLUDES, overlay.getExcludes() ) ) |
| 144 | |
{ |
| 145 | 27 | overlay.setIncludes( defaultIncludes ); |
| 146 | 27 | overlay.setExcludes( defaultExcludes ); |
| 147 | |
} |
| 148 | |
|
| 149 | 33 | final Artifact artifact = getAssociatedArtifact( overlay ); |
| 150 | 32 | if ( artifact != null ) |
| 151 | |
{ |
| 152 | 28 | configuredWarArtifacts.add( artifact ); |
| 153 | 28 | overlay.setArtifact( artifact ); |
| 154 | |
} |
| 155 | 32 | } |
| 156 | |
|
| 157 | |
|
| 158 | 65 | final Iterator it2 = artifactsOverlays.iterator(); |
| 159 | 99 | while ( it2.hasNext() ) |
| 160 | |
{ |
| 161 | 34 | Artifact artifact = (Artifact) it2.next(); |
| 162 | 34 | if ( !configuredWarArtifacts.contains( artifact ) ) |
| 163 | |
{ |
| 164 | |
|
| 165 | |
|
| 166 | 11 | overlays.add( new DefaultOverlay( artifact, defaultIncludes, defaultExcludes ) ); |
| 167 | |
} |
| 168 | 34 | } |
| 169 | |
|
| 170 | |
|
| 171 | 65 | final Iterator it3 = overlays.iterator(); |
| 172 | 98 | while ( it3.hasNext() ) |
| 173 | |
{ |
| 174 | 37 | Overlay overlay = (Overlay) it3.next(); |
| 175 | 37 | if ( overlay.equals( currentProjectOverlay ) ) |
| 176 | |
{ |
| 177 | 4 | return; |
| 178 | |
} |
| 179 | 33 | } |
| 180 | 61 | overlays.add( 0, currentProjectOverlay ); |
| 181 | 61 | } |
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
Artifact getAssociatedArtifact( final Overlay overlay ) |
| 196 | |
throws InvalidOverlayConfigurationException |
| 197 | |
{ |
| 198 | 33 | if ( overlay.isCurrentProject() ) |
| 199 | |
{ |
| 200 | 4 | return null; |
| 201 | |
} |
| 202 | |
|
| 203 | 29 | for ( Iterator iterator = artifactsOverlays.iterator(); iterator.hasNext(); ) |
| 204 | |
{ |
| 205 | |
|
| 206 | 46 | Artifact artifact = (Artifact) iterator.next(); |
| 207 | 46 | if ( compareOverlayWithArtifact( overlay, artifact ) ) |
| 208 | |
{ |
| 209 | 25 | return artifact; |
| 210 | |
} |
| 211 | 21 | } |
| 212 | |
|
| 213 | |
|
| 214 | 4 | Set projectArtifacts = this.project.getDependencyArtifacts(); |
| 215 | 4 | if ( projectArtifacts != null ) |
| 216 | |
{ |
| 217 | 3 | for ( Iterator iterator = projectArtifacts.iterator(); iterator.hasNext(); ) |
| 218 | |
{ |
| 219 | 3 | Artifact artifact = (Artifact) iterator.next(); |
| 220 | 3 | if ( compareOverlayWithArtifact( overlay, artifact ) ) |
| 221 | |
{ |
| 222 | 3 | return artifact; |
| 223 | |
} |
| 224 | 0 | } |
| 225 | |
} |
| 226 | 1 | throw new InvalidOverlayConfigurationException( |
| 227 | |
"overlay [" + overlay + "] is not a dependency of the project." ); |
| 228 | |
|
| 229 | |
} |
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
private boolean compareOverlayWithArtifact( Overlay overlay, Artifact artifact ) |
| 238 | |
{ |
| 239 | 49 | return ( StringUtils.equals( overlay.getGroupId(), artifact.getGroupId() ) |
| 240 | |
&& StringUtils.equals( overlay.getArtifactId(), artifact.getArtifactId() ) |
| 241 | |
&& StringUtils.equals( overlay.getType(), artifact.getType() ) |
| 242 | |
&& StringUtils.equals( overlay.getClassifier(), artifact.getClassifier() ) ); |
| 243 | |
} |
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
private List getOverlaysAsArtifacts() |
| 252 | |
{ |
| 253 | 66 | ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME ); |
| 254 | 66 | final Set artifacts = project.getArtifacts(); |
| 255 | 66 | final Iterator it = artifacts.iterator(); |
| 256 | |
|
| 257 | 66 | final List result = new ArrayList(); |
| 258 | 126 | while ( it.hasNext() ) |
| 259 | |
{ |
| 260 | 60 | Artifact artifact = (Artifact) it.next(); |
| 261 | 60 | if ( !artifact.isOptional() && filter.include( artifact ) && ( "war".equals( artifact.getType() ) ) ) |
| 262 | |
{ |
| 263 | 35 | result.add( artifact ); |
| 264 | |
} |
| 265 | 60 | } |
| 266 | 66 | return result; |
| 267 | |
} |
| 268 | |
} |