I have many more classes in the project but for now please consider only A, B and C classes.
abstract public class A {...}
public class B extends A {...}
public class C extends A {...}
Then later I have a code, say in class D, like this
A a = new B();
//A a = new C();
//use a's methods
So my question now is how to easily configure in Eclipse building of two separate JARs. First one should have B.class included and C.class excluded and code as A a = new B(); The second one should have C.class included and B.class excluded and code as A a = new C();
I do not know many things about Ant and Maven. Do I need to use them in this case?
Maybe, something wrong with my design, if so, please let me know.
What you want to is mainly a code loading problem, not an Ant problem.
Just the two statements you presented for creating a new class instance:
The constructors are called using static code. Ant can not change the code, therefore the only way with Ant I see is generating a Factory class with Ant as part of the build process, depending if
AorCis included into the JARBut that would result in a project that can no longer be used directly in Eclipse as the original source code in the Eclipse project misses the factor class.
A IMHO better approach is dynamic class loading (may be combined with reflection). You can automatically search for classes that extend
Aor you add a configuration file/info to the JAR specifying which class to create (e.g. properties file).Place the properties file in the src folder with the Java file and load it via
this.getClass().getResource("myclass.config");The config file can contain the class name that should used for creating a new instance.
The following code snipped assumes that
BandCboth have a public constructor that does not take any argument: