Code Monkey home page Code Monkey logo

Comments (3)

nosan avatar nosan commented on July 18, 2024 1

Hi @smiklosovic
Unfortunately, there is no easy way to do this. I can suggest only this solution:

public class ClassPathTest {

	@Test
	void shouldAddCustomLibraries() {
		EmbeddedCassandraFactory cassandraFactory = new EmbeddedCassandraFactory();
		cassandraFactory.setArtifact(new CustomArtifact(Version.of("3.11.6")));
		Cassandra cassandra = cassandraFactory.create();
		try {
			cassandra.start();
		}
		finally {
			cassandra.stop();
		}

	}

	private static final class CustomArtifact implements Artifact {

		private final Artifact delegate;

		private CustomArtifact(Version version) {
			this.delegate = Artifact.ofVersion(version);
		}

		@Override
		public Distribution getDistribution() throws IOException {
			Distribution distribution = this.delegate.getDistribution();
			Path destination = Files.createTempDirectory("apache-cassandra-" + distribution.getVersion());
			//clean up this directory on jvm exit
			Runtime.getRuntime().addShutdownHook(new Thread(() -> {
				try {
					FileUtils.delete(destination);
				}
				catch (IOException ex) {
					//ignore;
				}
			}));

			//copy everything from an original directory except javadoc, doc..
			FileUtils.copy(distribution.getDirectory(), destination, (path, attributes) -> {
				if (attributes.isDirectory()) {
					String name = path.getFileName().toString().toLowerCase(Locale.ENGLISH);
					return !name.equals("javadoc") && !name.equals("doc");
				}
				return true;
			});
			addLibraries(destination);
			return new DefaultDistribution(distribution.getVersion(), destination);
		}

		private void addLibraries(Path destination) throws IOException  {
			Path libDir = destination.resolve("lib");
			//copy libraries...
			try (InputStream resourceAsStream = CustomArtifact.class.getResourceAsStream("/mylib.jar")) {
				Files.copy(resourceAsStream, libDir.resolve("mylib.jar"));
			}
		}

	}

}

from embedded-cassandra.

nosan avatar nosan commented on July 18, 2024

@smiklosovic

According to cassndra.in.sh it is possible to extend classpath via EXTRA_CLASSPATH environment variable, so in your case, you can do something like this:

EmbeddedCassandraFactory cassandraFactory = new EmbeddedCassandraFactory();
cassandraFactory.getEnvironmentVariables().put("EXTRA_CLASSPATH", "path to your libraries")

from embedded-cassandra.

smiklosovic avatar smiklosovic commented on July 18, 2024

@nosan nice one, I invented my own solution, similar to yours, I am just adding that JAR directly into distribution dir so it starts with that automatically, however your original solution is cleaner. The second way to do it is the best and the simplest one.

from embedded-cassandra.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.