• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Reading Classpath Of My App From My Maven Plugin

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys !. I am building a Maven plugin to read the classpath from my webapp and then filter if the class is a controller or a service or something else. I could read the "/src/main/java" dir but I wasnt able to get a class intance from the .java files as I keep on getting "ClassNotFoundException". How could I bypass this and get this instance ?. Is there an object from the plugincontext that allows this ?.
For example: In my WebApp, I could have:
com.sports.soccer.ManagerController

From my maven plugin, I would need to do something like:
Class.forName("com.sports.soccer.ManagerController").isAnnotationPresent(Controller.class)

To check if the class is a controller !. Obviously this is an example... I would be reading the classpath and the amount of controllers would not be always the same from project to project....
 
Sheriff
Posts: 6030
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how to create a Maven plugin, but if you're looking for class files as opposed to source files, the Maven default for that folder is <project_dir>/target/classes.
 
AgustinC Cardeilhac Banus
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Knute Storm. Thankx for your reply. No, I wouldnt be needing to access that dir. What I am needing is to create a "Class" instance of every RestController / Controller, so as to get or obtain metadata of it by reflection.

I could read the dir "src/main/java" but I am not able to create the class instances. Its like being and not being inside the context of my webapp.
 
Sheriff
Posts: 21759
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
During what Maven phase is your plugin running? The classes need to be compiled first, so you would need the process-classes or later phase. You can see the phases here (among others).
 
AgustinC Cardeilhac Banus
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob. I am trying to run my maven plugin as a standalone one... No relation to any maven phase... Should I change this in order to be able to load them in class files ?.
 
AgustinC Cardeilhac Banus
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to load a class from my hosted application, but a simple one. Zero related dependencies. When I try to load a controller, it seems that it tries to load the associated dependencies and the loading fails.
Please cheack the attached picture...
 
AgustinC Cardeilhac Banus
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys... I am currently doing my gym routine but I was able to to solve the issue. I will post a picture hosted in my google drive as soon as I get to my house. Thankx for all the replies...
 
AgustinC Cardeilhac Banus
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here it's me again... The thing I did to solve the issue was the following:
Considering the following image:
Raw Code

I made a code to load a controller class with all the related dependencies. To avoid the need of the user to provide parameters, I did the following:
1) Read the application POM file and get the 'dependencies' tag.
2) Get a reference to the Maven Local Repo and with all the related dependencies from the application (Point 1), I got a list (List<File>) of JARS needed to load the classes annotated with "Controller" or "RestController".
3) Create a "URLClassLoader" object which receives an array of type "URL[]" (which would I would be getting by applying the intermediate operation "map" and doing to every file ".toURL()").
4) Then, when loading a controller in the following way ("com.sports.soccer.PlayerController"), the loading won't fail as the JAR dependencies would be load also. In the URL array from point 3, it should be added a reference to the bytecode dir of the app.

Thankx for all the help.
 
AgustinC Cardeilhac Banus
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously, the code in the image needs some structure and order, but at least I could manage to to see what was the magic needed with yours help. Thankx again
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!