How to find out the classpath used to load a custom task

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to find out the classpath used to load a custom task

Al Le
Hello,

I have created a JAR that contains (among other things) a java class
with the 'main' method, i.e. it can be executed from the command line or
using the 'java' task.

The program can take many parameters on the command line; the parameters
have not very nice names (for some historical reasons).

To make the usage of the program from within ant a little bit easier,
I've also created a custom ant task which has some nicely named nested
elements that more or less correspont to the program parameters.

When the task is executed, it internally creates an instance of the Java
task (passing itself as the owner in the constructor), sets the
classname attribute to the name of the program's class, adds the command
line parameters, and executes the Java task.

I.e. it effectively translates its own definition to a call of the Java
task.

This is a code snippet from my custom task:


     @Override
     public void execute() throws BuildException {
         Task progExecutionTask = getProgramExecutionTask();
         progExecutionTask.execute();
     }

     private Task getProgramExecutionTask() {
         Java javaTask = new Java(this);
         javaTask.init();

         javaTask.setClassname(MyProgram.class.getName());

         // Set the program parameters
         CommandlineJava cmdLine = javaTask.getCommandLine();

         // programArgumentsList is an instance variable in my custom
         // task; it gets filled when addConfiguredXyz methods are called
         for (String arg : programArgumentsList) {
             cmdLine.createArgument().setValue(arg);
         }

         return javaTask;
     }


My problem is that I also have to set the classpath attribute for the
internal Java task. And I'd like to set it automatically to the same
classpath that was used for loading of my custom task (since the 'main'
class and the custom task are in the same JAR).

Is it possible (and how?) to get the classpath that was used to load a
custom task, from withing that custom task?

I hope you understand what I mean.

Thank you!

Al

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: How to find out the classpath used to load a custom task

Al Le
PS. Since the custom task is in the same JAR as the main class, I could
just call the 'main' method from the task's 'execute' method. I'd still
prefer to do it via the Java task to be able to fork etc.


Am 08.12.2018 um 21:57 schrieb Al Le:

> Hello,
>
> I have created a JAR that contains (among other things) a java class
> with the 'main' method, i.e. it can be executed from the command line or
> using the 'java' task.
>
> The program can take many parameters on the command line; the parameters
> have not very nice names (for some historical reasons).
>
> To make the usage of the program from within ant a little bit easier,
> I've also created a custom ant task which has some nicely named nested
> elements that more or less correspont to the program parameters.
>
> When the task is executed, it internally creates an instance of the Java
> task (passing itself as the owner in the constructor), sets the
> classname attribute to the name of the program's class, adds the command
> line parameters, and executes the Java task.
>
> I.e. it effectively translates its own definition to a call of the Java
> task.
>
> This is a code snippet from my custom task:
>
>
>      @Override
>      public void execute() throws BuildException {
>          Task progExecutionTask = getProgramExecutionTask();
>          progExecutionTask.execute();
>      }
>
>      private Task getProgramExecutionTask() {
>          Java javaTask = new Java(this);
>          javaTask.init();
>
>          javaTask.setClassname(MyProgram.class.getName());
>
>          // Set the program parameters
>          CommandlineJava cmdLine = javaTask.getCommandLine();
>
>          // programArgumentsList is an instance variable in my custom
>          // task; it gets filled when addConfiguredXyz methods are called
>          for (String arg : programArgumentsList) {
>              cmdLine.createArgument().setValue(arg);
>          }
>
>          return javaTask;
>      }
>
>
> My problem is that I also have to set the classpath attribute for the
> internal Java task. And I'd like to set it automatically to the same
> classpath that was used for loading of my custom task (since the 'main'
> class and the custom task are in the same JAR).
>
> Is it possible (and how?) to get the classpath that was used to load a
> custom task, from withing that custom task?
>
> I hope you understand what I mean.
>
> Thank you!
>
> Al

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]