Skip to content

Commit b06226a

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 b06226a

File tree

5 files changed

+63
-357
lines changed

5 files changed

+63
-357
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: 39 additions & 34 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,11 +24,8 @@
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;
29-
import org.slf4j.Logger;
30-
import org.slf4j.LoggerFactory;
3129

3230
/**
3331
* Filesystem based non-incremental build context implementation which behaves
@@ -48,10 +46,21 @@
4846
public class DefaultBuildContext implements BuildContext {
4947

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

5766
/**
@@ -61,7 +70,7 @@ public boolean hasDelta(String relpath) {
6170
* @return a boolean.
6271
*/
6372
public boolean hasDelta(File file) {
64-
return true;
73+
return legacy.hasDelta(file);
6574
}
6675

6776
/**
@@ -71,34 +80,44 @@ public boolean hasDelta(File file) {
7180
* @return a boolean.
7281
*/
7382
public boolean hasDelta(List<String> relpaths) {
74-
return true;
83+
return legacy.hasDelta(relpaths);
7584
}
7685

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

82103
/** {@inheritDoc} */
83104
public Scanner newScanner(File basedir) {
84-
DirectoryScanner ds = new DirectoryScanner();
85-
ds.setBasedir(basedir);
86-
return ds;
105+
return legacy.newScanner(basedir);
87106
}
88107

89108
/** {@inheritDoc} */
90109
public void refresh(File file) {
91-
// do nothing
110+
legacy.refresh(file);
92111
}
93112

94113
/** {@inheritDoc} */
95114
public Scanner newDeleteScanner(File basedir) {
96-
return new EmptyScanner(basedir);
115+
return legacy.newDeleteScanner(basedir);
97116
}
98117

99118
/** {@inheritDoc} */
100119
public Scanner newScanner(File basedir, boolean ignoreDelta) {
101-
return newScanner(basedir);
120+
return legacy.newScanner(basedir, ignoreDelta);
102121
}
103122

104123
/**
@@ -107,7 +126,7 @@ public Scanner newScanner(File basedir, boolean ignoreDelta) {
107126
* @return a boolean.
108127
*/
109128
public boolean isIncremental() {
110-
return false;
129+
return legacy.isIncremental();
111130
}
112131

113132
/** {@inheritDoc} */
@@ -120,10 +139,6 @@ public void setValue(String key, Object value) {
120139
contextMap.put(key, value);
121140
}
122141

123-
private String getMessage(File file, int line, int column, String message) {
124-
return file.getAbsolutePath() + " [" + line + ':' + column + "]: " + message;
125-
}
126-
127142
/** {@inheritDoc} */
128143
public void addError(File file, int line, int column, String message, Throwable cause) {
129144
addMessage(file, line, column, message, SEVERITY_ERROR, cause);
@@ -136,26 +151,16 @@ public void addWarning(File file, int line, int column, String message, Throwabl
136151

137152
/** {@inheritDoc} */
138153
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;
146-
}
147-
throw new IllegalArgumentException("severity=" + severity);
154+
legacy.addMessage(file, line, column, message, severity, cause);
148155
}
149156

150157
/** {@inheritDoc} */
151-
public void removeMessages(File file) {}
158+
public void removeMessages(File file) {
159+
legacy.removeMessages(file);
160+
}
152161

153162
/** {@inheritDoc} */
154163
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();
164+
return legacy.isUptodate(target, source);
160165
}
161166
}

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)