Skip to content

Commit 7f5be6f

Browse files
author
Christoph Läubrich
committed
Let the DefaultBuildContext delegate to the legacy build-api
Currently there is a problem that if a maven-plugin wants to upgrade to the newer API artifact it looses backward-compatibility to older implementors of the API instantly. This changes the DefaultBuildContext in a way that allows it to behave backward-compatible in this case: 1) it gets injected the old implementation 2) it delegates all relevant calls to the legacy 3) it contains a feature-switch that is able to detect if the default-legacy-api is used and therefore we can exchange behavior with a new default or need to still to delegate to a custom implementation.
1 parent 0ead773 commit 7f5be6f

File tree

5 files changed

+81
-354
lines changed

5 files changed

+81
-354
lines changed

pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,30 @@ See the Apache License Version 2.0 for the specific language governing permissio
4646
<artifactId>slf4j-api</artifactId>
4747
<version>1.7.36</version>
4848
</dependency>
49+
<!-- The depednecies are only to support the legacy API therefore we exclude anything as we only need the API/classes -->
50+
<dependency>
51+
<groupId>org.sonatype.plexus</groupId>
52+
<artifactId>plexus-build-api</artifactId>
53+
<version>0.0.7</version>
54+
<exclusions>
55+
<exclusion>
56+
<groupId>*</groupId>
57+
<artifactId>*</artifactId>
58+
</exclusion>
59+
</exclusions>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.eclipse.sisu</groupId>
63+
<artifactId>org.eclipse.sisu.plexus</artifactId>
64+
<version>0.3.5</version>
65+
<exclusions>
66+
<exclusion>
67+
<groupId>*</groupId>
68+
<artifactId>*</artifactId>
69+
</exclusion>
70+
</exclusions>
71+
</dependency>
72+
4973
</dependencies>
5074

5175
<build>

src/main/java/org/codehaus/plexus/build/DefaultBuildContext.java

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package org.codehaus.plexus.build;
1515

16+
import javax.inject.Inject;
1617
import javax.inject.Named;
1718
import javax.inject.Singleton;
1819

@@ -23,7 +24,6 @@
2324
import java.util.Map;
2425
import java.util.concurrent.ConcurrentHashMap;
2526

26-
import org.codehaus.plexus.util.DirectoryScanner;
2727
import org.codehaus.plexus.util.Scanner;
2828
import org.codehaus.plexus.util.io.CachingOutputStream;
2929
import org.slf4j.Logger;
@@ -47,11 +47,24 @@
4747
@Singleton
4848
public class DefaultBuildContext implements BuildContext {
4949

50-
private final Map<String, Object> contextMap = new ConcurrentHashMap<>();
5150
private final Logger logger = LoggerFactory.getLogger(DefaultBuildContext.class);
51+
52+
private final Map<String, Object> contextMap = new ConcurrentHashMap<>();
53+
private org.sonatype.plexus.build.incremental.BuildContext legacy;
54+
55+
/**
56+
* @param legacy the legacy API we delegate to by default, this allow us to
57+
* support "older" plugins and implementors of the API while still
58+
* having a way to move forward!
59+
*/
60+
@Inject
61+
public DefaultBuildContext(org.sonatype.plexus.build.incremental.BuildContext legacy) {
62+
this.legacy = legacy;
63+
}
64+
5265
/** {@inheritDoc} */
5366
public boolean hasDelta(String relpath) {
54-
return true;
67+
return legacy.hasDelta(relpath);
5568
}
5669

5770
/**
@@ -61,7 +74,7 @@ public boolean hasDelta(String relpath) {
6174
* @return a boolean.
6275
*/
6376
public boolean hasDelta(File file) {
64-
return true;
77+
return legacy.hasDelta(file);
6578
}
6679

6780
/**
@@ -71,34 +84,44 @@ public boolean hasDelta(File file) {
7184
* @return a boolean.
7285
*/
7386
public boolean hasDelta(List<String> relpaths) {
74-
return true;
87+
return legacy.hasDelta(relpaths);
7588
}
7689

7790
/** {@inheritDoc} */
7891
public OutputStream newFileOutputStream(File file) throws IOException {
79-
return new CachingOutputStream(file.toPath());
92+
if (isDefaultImplementation()) {
93+
return new CachingOutputStream(file.toPath());
94+
}
95+
return legacy.newFileOutputStream(file);
96+
}
97+
98+
/**
99+
* @return <code>true</code> if the legacy is the default implementation and we
100+
* can safely override/change behavior here, or <code>false</code> if a
101+
* custom implementation is used and full delegation is required.
102+
*/
103+
private boolean isDefaultImplementation() {
104+
return legacy.getClass().equals(org.sonatype.plexus.build.incremental.DefaultBuildContext.class);
80105
}
81106

82107
/** {@inheritDoc} */
83108
public Scanner newScanner(File basedir) {
84-
DirectoryScanner ds = new DirectoryScanner();
85-
ds.setBasedir(basedir);
86-
return ds;
109+
return legacy.newScanner(basedir);
87110
}
88111

89112
/** {@inheritDoc} */
90113
public void refresh(File file) {
91-
// do nothing
114+
legacy.refresh(file);
92115
}
93116

94117
/** {@inheritDoc} */
95118
public Scanner newDeleteScanner(File basedir) {
96-
return new EmptyScanner(basedir);
119+
return legacy.newDeleteScanner(basedir);
97120
}
98121

99122
/** {@inheritDoc} */
100123
public Scanner newScanner(File basedir, boolean ignoreDelta) {
101-
return newScanner(basedir);
124+
return legacy.newScanner(basedir, ignoreDelta);
102125
}
103126

104127
/**
@@ -107,7 +130,7 @@ public Scanner newScanner(File basedir, boolean ignoreDelta) {
107130
* @return a boolean.
108131
*/
109132
public boolean isIncremental() {
110-
return false;
133+
return legacy.isIncremental();
111134
}
112135

113136
/** {@inheritDoc} */
@@ -120,10 +143,6 @@ public void setValue(String key, Object value) {
120143
contextMap.put(key, value);
121144
}
122145

123-
private String getMessage(File file, int line, int column, String message) {
124-
return file.getAbsolutePath() + " [" + line + ':' + column + "]: " + message;
125-
}
126-
127146
/** {@inheritDoc} */
128147
public void addError(File file, int line, int column, String message, Throwable cause) {
129148
addMessage(file, line, column, message, SEVERITY_ERROR, cause);
@@ -134,28 +153,35 @@ public void addWarning(File file, int line, int column, String message, Throwabl
134153
addMessage(file, line, column, message, SEVERITY_WARNING, cause);
135154
}
136155

156+
private String getMessage(File file, int line, int column, String message) {
157+
return file.getAbsolutePath() + " [" + line + ':' + column + "]: " + message;
158+
}
159+
137160
/** {@inheritDoc} */
138161
public void addMessage(File file, int line, int column, String message, int severity, Throwable cause) {
139-
switch (severity) {
140-
case BuildContext.SEVERITY_ERROR:
141-
logger.error(getMessage(file, line, column, message), cause);
142-
return;
143-
case BuildContext.SEVERITY_WARNING:
144-
logger.warn(getMessage(file, line, column, message), cause);
145-
return;
162+
if (isDefaultImplementation()) {
163+
switch (severity) {
164+
case BuildContext.SEVERITY_ERROR:
165+
logger.error(getMessage(file, line, column, message), cause);
166+
return;
167+
case BuildContext.SEVERITY_WARNING:
168+
logger.warn(getMessage(file, line, column, message), cause);
169+
return;
170+
default:
171+
logger.debug(getMessage(file, line, column, message), cause);
172+
return;
173+
}
146174
}
147-
throw new IllegalArgumentException("severity=" + severity);
175+
legacy.addMessage(file, line, column, message, severity, cause);
148176
}
149177

150178
/** {@inheritDoc} */
151-
public void removeMessages(File file) {}
179+
public void removeMessages(File file) {
180+
legacy.removeMessages(file);
181+
}
152182

153183
/** {@inheritDoc} */
154184
public boolean isUptodate(File target, File source) {
155-
return target != null
156-
&& target.exists()
157-
&& source != null
158-
&& source.exists()
159-
&& target.lastModified() > source.lastModified();
185+
return legacy.isUptodate(target, source);
160186
}
161187
}

src/main/java/org/codehaus/plexus/build/EmptyScanner.java

Lines changed: 0 additions & 93 deletions
This file was deleted.

src/test/java/org/codehaus/plexus/build/test/TestFullBuildContext.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)