Skip to content

Commit 9a9fb61

Browse files
Christoph Läubrichgnodet
Christoph Läubrich
authored andcommitted
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 9a9fb61

File tree

5 files changed

+89
-354
lines changed

5 files changed

+89
-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.9.0.M2</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: 65 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,7 @@
2324
import java.util.Map;
2425
import java.util.concurrent.ConcurrentHashMap;
2526

26-
import org.codehaus.plexus.util.DirectoryScanner;
27+
import org.codehaus.plexus.logging.AbstractLogEnabled;
2728
import org.codehaus.plexus.util.Scanner;
2829
import org.codehaus.plexus.util.io.CachingOutputStream;
2930
import org.slf4j.Logger;
@@ -47,11 +48,28 @@
4748
@Singleton
4849
public class DefaultBuildContext implements BuildContext {
4950

50-
private final Map<String, Object> contextMap = new ConcurrentHashMap<>();
5151
private final Logger logger = LoggerFactory.getLogger(DefaultBuildContext.class);
52+
// the legacy API requires the AbstractLogEnabled we just have it here to get
53+
// compile errors in case it is missing from the classpath!
54+
@SuppressWarnings("unused")
55+
private static final AbstractLogEnabled DUMMY = null;
56+
57+
private final Map<String, Object> contextMap = new ConcurrentHashMap<>();
58+
private org.sonatype.plexus.build.incremental.BuildContext legacy;
59+
60+
/**
61+
* @param legacy the legacy API we delegate to by default, this allow us to
62+
* support "older" plugins and implementors of the API while still
63+
* having a way to move forward!
64+
*/
65+
@Inject
66+
public DefaultBuildContext(org.sonatype.plexus.build.incremental.BuildContext legacy) {
67+
this.legacy = legacy;
68+
}
69+
5270
/** {@inheritDoc} */
5371
public boolean hasDelta(String relpath) {
54-
return true;
72+
return legacy.hasDelta(relpath);
5573
}
5674

5775
/**
@@ -61,7 +79,7 @@ public boolean hasDelta(String relpath) {
6179
* @return a boolean.
6280
*/
6381
public boolean hasDelta(File file) {
64-
return true;
82+
return legacy.hasDelta(file);
6583
}
6684

6785
/**
@@ -71,34 +89,44 @@ public boolean hasDelta(File file) {
7189
* @return a boolean.
7290
*/
7391
public boolean hasDelta(List<String> relpaths) {
74-
return true;
92+
return legacy.hasDelta(relpaths);
7593
}
7694

7795
/** {@inheritDoc} */
7896
public OutputStream newFileOutputStream(File file) throws IOException {
79-
return new CachingOutputStream(file.toPath());
97+
if (isDefaultImplementation()) {
98+
return new CachingOutputStream(file.toPath());
99+
}
100+
return legacy.newFileOutputStream(file);
101+
}
102+
103+
/**
104+
* @return <code>true</code> if the legacy is the default implementation and we
105+
* can safely override/change behavior here, or <code>false</code> if a
106+
* custom implementation is used and full delegation is required.
107+
*/
108+
private boolean isDefaultImplementation() {
109+
return legacy.getClass().equals(org.sonatype.plexus.build.incremental.DefaultBuildContext.class);
80110
}
81111

82112
/** {@inheritDoc} */
83113
public Scanner newScanner(File basedir) {
84-
DirectoryScanner ds = new DirectoryScanner();
85-
ds.setBasedir(basedir);
86-
return ds;
114+
return legacy.newScanner(basedir);
87115
}
88116

89117
/** {@inheritDoc} */
90118
public void refresh(File file) {
91-
// do nothing
119+
legacy.refresh(file);
92120
}
93121

94122
/** {@inheritDoc} */
95123
public Scanner newDeleteScanner(File basedir) {
96-
return new EmptyScanner(basedir);
124+
return legacy.newDeleteScanner(basedir);
97125
}
98126

99127
/** {@inheritDoc} */
100128
public Scanner newScanner(File basedir, boolean ignoreDelta) {
101-
return newScanner(basedir);
129+
return legacy.newScanner(basedir, ignoreDelta);
102130
}
103131

104132
/**
@@ -107,7 +135,7 @@ public Scanner newScanner(File basedir, boolean ignoreDelta) {
107135
* @return a boolean.
108136
*/
109137
public boolean isIncremental() {
110-
return false;
138+
return legacy.isIncremental();
111139
}
112140

113141
/** {@inheritDoc} */
@@ -120,10 +148,6 @@ public void setValue(String key, Object value) {
120148
contextMap.put(key, value);
121149
}
122150

123-
private String getMessage(File file, int line, int column, String message) {
124-
return file.getAbsolutePath() + " [" + line + ':' + column + "]: " + message;
125-
}
126-
127151
/** {@inheritDoc} */
128152
public void addError(File file, int line, int column, String message, Throwable cause) {
129153
addMessage(file, line, column, message, SEVERITY_ERROR, cause);
@@ -134,28 +158,38 @@ public void addWarning(File file, int line, int column, String message, Throwabl
134158
addMessage(file, line, column, message, SEVERITY_WARNING, cause);
135159
}
136160

161+
private String getMessage(File file, int line, int column, String message) {
162+
return file.getAbsolutePath() + " [" + line + ':' + column + "]: " + message;
163+
}
164+
137165
/** {@inheritDoc} */
138166
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;
167+
if (isDefaultImplementation()) {
168+
switch (severity) {
169+
case BuildContext.SEVERITY_ERROR:
170+
logger.error(getMessage(file, line, column, message), cause);
171+
return;
172+
case BuildContext.SEVERITY_WARNING:
173+
logger.warn(getMessage(file, line, column, message), cause);
174+
return;
175+
default:
176+
logger.debug(getMessage(file, line, column, message), cause);
177+
return;
178+
}
146179
}
147-
throw new IllegalArgumentException("severity=" + severity);
180+
legacy.addMessage(file, line, column, message, severity, cause);
148181
}
149182

150183
/** {@inheritDoc} */
151-
public void removeMessages(File file) {}
184+
public void removeMessages(File file) {
185+
if (isDefaultImplementation()) {
186+
return;
187+
}
188+
legacy.removeMessages(file);
189+
}
152190

153191
/** {@inheritDoc} */
154192
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();
193+
return legacy.isUptodate(target, source);
160194
}
161195
}

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)