Skip to content
62 changes: 50 additions & 12 deletions biz.aQute.bndlib/src/aQute/bnd/build/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,8 @@ public List<Container> getBundles(Strategy strategyx, String spec, String source
decorator.decorate(bundles, true);
}

List<RepositoryPlugin> repos = getRepositories(repoTagsBySource(source));

List<Container> result = new ArrayList<>();
try {
for (Entry<String, Attrs> entry : bundles.entrySet()) {
Expand All @@ -649,12 +651,12 @@ public List<Container> getBundles(Strategy strategyx, String spec, String source
boolean triedGetBundle = false;

if (bsn.indexOf('*') >= 0) {
return getBundlesWildcard(bsn, versionRange, strategyx, attrs);
return getBundlesWildcard(bsn, versionRange, strategyx, attrs, repos);
}

if (versionRange != null) {
if (versionRange.equals(VERSION_ATTR_LATEST) || versionRange.equals(VERSION_ATTR_SNAPSHOT)) {
found = getBundle(bsn, versionRange, strategyx, attrs);
found = getBundle(bsn, versionRange, strategyx, attrs, repos);
triedGetBundle = true;
}
}
Expand Down Expand Up @@ -693,7 +695,7 @@ public List<Container> getBundles(Strategy strategyx, String spec, String source
found = new Container(this, bsn, "file", Container.TYPE.EXTERNAL, f, error, attrs, null);
}
} else if (!triedGetBundle) {
found = getBundle(bsn, versionRange, strategyx, attrs);
found = getBundle(bsn, versionRange, strategyx, attrs, repos);
}
}

Expand Down Expand Up @@ -727,6 +729,25 @@ public List<Container> getBundles(Strategy strategyx, String spec, String source
return result;
}

private String[] repoTagsBySource(String source) {

if (source == null) {
return null;
}

if (Constants.BUILDPATH.equals(source)) {
return new String[] {
Constants.REPOTAGS_COMPILE
};
} else if (Constants.TESTPATH.equals(source)) {
return new String[] {
Constants.REPOTAGS_COMPILE, Constants.REPOTAGS_TEST
};
}
// TODO how to determine tag "debug"
return null;
}

/**
* Just calls a new method with a default parm.
*
Expand All @@ -750,6 +771,13 @@ Collection<Container> getBundles(Strategy strategy, String spec) throws Exceptio
public List<Container> getBundlesWildcard(String bsnPattern, String range, Strategy strategyx,
Map<String, String> attrs) throws Exception {

List<RepositoryPlugin> plugins = getRepositories();
return getBundlesWildcard(bsnPattern, range, strategyx, attrs, plugins);
}

private List<Container> getBundlesWildcard(String bsnPattern, String range, Strategy strategyx,
Map<String, String> attrs, List<RepositoryPlugin> repos) throws Exception {

if (VERSION_ATTR_SNAPSHOT.equals(range) || VERSION_ATTR_PROJECT.equals(range))
return Collections.singletonList(new Container(this, bsnPattern, range, TYPE.ERROR, null,
"Cannot use snapshot or project version with wildcard matches", null, null));
Expand All @@ -773,8 +801,7 @@ public List<Container> getBundlesWildcard(String bsnPattern, String range, Strat

SortedMap<String, Pair<Version, RepositoryPlugin>> providerMap = new TreeMap<>();

List<RepositoryPlugin> plugins = getRepositories();
for (RepositoryPlugin plugin : plugins) {
for (RepositoryPlugin plugin : repos) {

if (repoFilter != null && !repoFilter.match(plugin))
continue;
Expand Down Expand Up @@ -822,11 +849,13 @@ public List<Container> getBundlesWildcard(String bsnPattern, String range, Strat

DownloadBlocker downloadBlocker = new DownloadBlocker(this);
File bundle = repo.get(bsn, version, attrs, downloadBlocker);

if (bundle != null && !bundle.getName()
.endsWith(".lib")) {
containers
.add(new Container(this, bsn, range, Container.TYPE.REPO, bundle, null, attrs, downloadBlocker));
}

}

return containers;
Expand Down Expand Up @@ -1253,6 +1282,14 @@ public void release(String name, boolean test) throws Exception {
public Container getBundle(String bsn, String range, Strategy strategy, Map<String, String> attrs)
throws Exception {

List<RepositoryPlugin> plugins = getRepositories();
return getBundle(bsn, range, strategy, attrs, plugins);

}

private Container getBundle(String bsn, String range, Strategy strategy, Map<String, String> attrs,
List<RepositoryPlugin> repos) throws Exception {

if (range == null)
range = "0";
if (attrs == null) {
Expand All @@ -1278,7 +1315,7 @@ public Container getBundle(String bsn, String range, Strategy strategy, Map<Stri
useStrategy = overrideStrategy(attrs, useStrategy);
RepoFilter repoFilter = parseRepoFilter(attrs);

List<RepositoryPlugin> plugins = getRepositories();


if (useStrategy == Strategy.EXACT) {
if (!Verifier.isVersion(range))
Expand All @@ -1288,7 +1325,8 @@ public Container getBundle(String bsn, String range, Strategy strategy, Map<Stri
// For an exact range we just iterate over the repos
// and return the first we find.
Version version = new Version(range);
for (RepositoryPlugin plugin : plugins) {

for (RepositoryPlugin plugin : repos) {
DownloadBlocker blocker = new DownloadBlocker(this);
File result = plugin.get(bsn, version, attrs, blocker);
if (result != null)
Expand All @@ -1304,7 +1342,8 @@ public Container getBundle(String bsn, String range, Strategy strategy, Map<Stri
// multiple repos we take the first

SortedMap<Version, RepositoryPlugin> versions = new TreeMap<>();
for (RepositoryPlugin plugin : plugins) {

for (RepositoryPlugin plugin : repos) {

if (repoFilter != null && !repoFilter.match(plugin))
continue;
Expand Down Expand Up @@ -1382,8 +1421,7 @@ public Container getBundle(String bsn, String range, Strategy strategy, Map<Stri
//

return new Container(this, bsn, range, Container.TYPE.ERROR, null,
bsn + ";version=" + range + " Not found in " + plugins, attrs, null);

bsn + ";version=" + range + " Not found in " + repos, attrs, null);
}

/**
Expand All @@ -1394,8 +1432,8 @@ public Container getBundle(String bsn, String range, Strategy strategy, Map<Stri
*
* @return a list of relevant repositories used by various methods.
*/
public List<RepositoryPlugin> getRepositories() {
return workspace.getRepositories();
public List<RepositoryPlugin> getRepositories(String... tags) {
return workspace.getRepositories(tags);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions biz.aQute.bndlib/src/aQute/bnd/build/Workspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
import aQute.bnd.service.repository.Prepare;
import aQute.bnd.service.repository.RepositoryDigest;
import aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor;
import aQute.bnd.service.tags.Tags;
import aQute.bnd.stream.MapStream;
import aQute.bnd.url.MultiURLConnectionHandler;
import aQute.bnd.util.home.Home;
Expand Down Expand Up @@ -720,6 +721,32 @@ public List<RepositoryPlugin> getRepositories() {
return data.repositories.get();
}

/**
* @param tags list tags to filter. <code>null</code> means all.
* @return matching repositories.
*/
public List<RepositoryPlugin> getRepositories(String... tags) {

if (tags == null) {
return data.repositories.get();
}

Tags activeTags = Tags.parse(getProperty(Constants.ACTIVETAGS), Tags.of(Constants.REPOTAGS_RESOLVE));

return data.repositories.get()
.stream()
.filter(repo -> {
Tags repotags = repo.getTags();

if (repotags == null || !activeTags.includesAny(tags) || repotags.includesAny(tags)) {
return true;
}

return false;
})
.toList();
}

private List<RepositoryPlugin> initRepositories() {
List<RepositoryPlugin> plugins = getPlugins(RepositoryPlugin.class);
for (RepositoryPlugin repo : plugins) {
Expand Down
6 changes: 5 additions & 1 deletion biz.aQute.bndlib/src/aQute/bnd/osgi/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,16 @@ public interface Constants {

String REMOTEWORKSPACE = "-remoteworkspace";

String ACTIVETAGS = "-activetags";
/**
* tag for repos which should be used for Resolving bundles. This is also
* tags for repos which should be used for Resolving bundles. This is also
* the default tag for all repos which not have specified tags (also for bc
* reasons)
*/
String REPOTAGS_RESOLVE = "resolve";
String REPOTAGS_COMPILE = "compile";
String REPOTAGS_TEST = "test";
String REPOTAGS_DEBUG = "debug";

String RUNBLACKLIST = "-runblacklist";
String RUNREQUIRES = "-runrequires";
Expand Down
23 changes: 22 additions & 1 deletion biz.aQute.bndlib/src/aQute/bnd/service/tags/Tags.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,24 @@ public String toString() {
return internalSet.toString();
}



/**
* Returns {@code true} if this entity should take part in <i>any</i> of the
* caller-requested tags.
*
* // @formatter:off
* Semantics – in precedence order
* --------------------------------
* 1. **Legacy wildcard** – if this entity has no tags at all, it matches
* every tag (backwards compatibility).
* 2. **Negative tag overrides** – if the entity contains a literal tag
* {@code "no" + tag} it is <b>excluded</b> from that phase, regardless
* of all other rules (example 'nocompile'.
* 3. **Positive tag match** – if the entity contains a tag that matches a
* requested glob, it is included.
* // @formatter:on
*
* @param tags (globs)
* @return <code>true</code> if any of the given tags is included in the
* current set of tags, otherwise returns <code>false</code>. Also
Expand All @@ -128,21 +145,25 @@ public boolean includesAny(String... tags) {
return true;
}


for (String tagGlob : tags) {

if (matchesAny(new Glob(tagGlob))) {
return true;
return true; // success
}
}

return false;

}


private boolean matchesAny(Glob glob) {
return internalSet.stream()
.anyMatch(s -> glob.matches(s));
}


/**
* @param name
* @return a Tags instance with the given tags.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import java.util.List;
import java.util.Map;

import org.assertj.core.api.SoftAssertions;
import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.osgi.service.repository.Repository;

import aQute.bnd.build.Workspace;
Expand All @@ -25,6 +28,7 @@
import aQute.lib.io.IO;
import aQute.maven.provider.FakeNexus;

@ExtendWith(SoftAssertionsExtension.class)
public class WorkspaceTest {
@InjectTemporaryDirectory
File tmp;
Expand Down Expand Up @@ -80,6 +84,10 @@ public void testEnv() throws Exception {
assertTrue(((Tagged) repo).getTags()
.includesAny(Constants.REPOTAGS_RESOLVE));

// same for 'compile' tag
List<Repository> compileRepos = workspace.getPlugins(Repository.class, Constants.REPOTAGS_RESOLVE);
assertEquals(repos, compileRepos);

}

@Test
Expand Down Expand Up @@ -146,4 +154,59 @@ public void testTagDisplay() {
assertEquals("foo", Tags.print(Tags.of(Tagged.EMPTY_TAGS, "foo")));
assertEquals("bar,foo", Tags.print(Tags.of(Tagged.EMPTY_TAGS, "foo", "bar")));
}

@Test
public void testTags(SoftAssertions softly) {

softly.assertThat(Tags.of()
.includesAny("resolve"))
.isTrue();

softly.assertThat(Tags.of()
.includesAny("compile"))
.isTrue();

softly.assertThat(Tags.of("resolve")
.includesAny("resolve"))
.isTrue();


softly.assertThat(Tags.of("resolve", "compile")
.includesAny("resolve"))
.isTrue();

softly.assertThat(Tags.of("resolve", "compile")
.includesAny("compile"))
.isTrue();

softly.assertThat(Tags.of("resolve", "compile")
.includesAny("compile", "resolve"))
.isTrue();

softly.assertThat(Tags.of("resolve", "compile")
.includesAny("resolve", "compile"))
.isTrue();

softly.assertThat(Tags.of("resolve", "compile", "somethingelse")
.includesAny("resolve", "compile"))
.isTrue();

// negative
softly.assertThat(Tags.of("resolve", "nocompile")
.includesAny("resolve"))
.isTrue();

softly.assertThat(Tags.of("resolve", "nocompile")
.includesAny("compile"))
.isFalse();

softly.assertThat(Tags.of("noresolve", "nocompile")
.includesAny("resolve"))
.isFalse();

softly.assertThat(Tags.of("noresolve", "nocompile")
.includesAny("compile"))
.isFalse();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ public MyBndrun(Workspace workspace, File propertiesFile) throws Exception {
}

@Override
public List<RepositoryPlugin> getRepositories() {
public List<RepositoryPlugin> getRepositories(String... tags) {
return getPlugins(RepositoryPlugin.class);
}

Expand Down
Loading