3

I'm developing a Spring MVC application and I use Maven. I'm currently trying to create some Java "scripts" (I'm not sure I should call them "scripts", I actually mean "Java classes with a main(String[] args) method" that can be callled on the command line) to do some work using Liquibase.

The "scripts" would load some context files from the Spring MVC application and have access to the properties files from the web application. They will thus be able to use the same database connection settings than the application, etc.

My question is: how to run those scripts?

Locally I'm using Eclipse and running those "scripts" is easy using "Run As / Java Application". But I want to be able to run them on my prod server too, on which I can't use eclipse to start them. What should I do to be able to run them there?

Should I export my application in another format than .war? Should I create an extra .jar from the project to be able to run the scripts using "java -jar"?

Should I create a separate project for those scripts? But then, would the scripts be able to load the required contexts from the Spring MVC application located in the other project?

Maybe there is an easy way to do this that I don't see?

UPDATE : One way I see I could achieve what I want would be by having the web application to expose web services as a door to the tasks to run. Then I could call those using wget or something, on the command line. But I'd prefere to be able to reach the classes directly, without the need to create web services.

electrotype
  • 8,342
  • 11
  • 59
  • 96
  • In which language are this scripts? Java, Java Script, ... – Ralph Mar 23 '12 at 08:18
  • Ralph, I use the term "script", but it may not be the correct one. I'm only talking about _Java_ here. I want to be able to run some Java classes containing a **main(String[] args)** method to make them perform tasks. I must be able to run those classes _on demand_ (using a shell command, "java -jar" or something) and those classes will have to load my application contexts to get the informations they need to perform their tasks. I call them "scripts" because they perform specific tasks, which are not part of the main application flow... And they are run individually, on demand. – electrotype Mar 23 '12 at 10:25
  • This sounds like it should really be a separate project. Why do they need access to the actual context and properties files? – bvulaj Mar 23 '12 at 12:52
  • BrandonV, I could copy the properties into another, separated, project and it would work for my current needs, indeed. But I would prefere to use the configuration from the main application itself, directly. Also, I have to admit my question is partly based on plain curiosity. I'd like to know it's it's actually possible! Can one invoke classes from a web project, on demand and from a shell/script, and have access to all configuration, context and properties files? – electrotype Mar 23 '12 at 13:12

2 Answers2

0

What's the purpose of your "java scripts"

  1. To do something periodically in bakcground. You might use the Quartz framework to trigger the job automatically. see Quartz scheduler
  2. To make sure something is working correctly. I ran a main() method in my class to see whether something is working correctly before I knew about junit. See junit
Southeast
  • 587
  • 2
  • 5
  • 11
  • I need those "scripts" to generate some Liquibase changeSets based on the previous version of my database and the current modifications done on Hibernate in the application. I want to generate those changeSets _on demand_, not on a regular basis, so I don't think Quartz jobs are a good fit. Also, this generation is certainly not a test, so junit is not the way to go. Finally, I need my "scripts" to be able to acccess and load all my application contexts using Spring. – electrotype Mar 23 '12 at 10:16
0

(First, I found a similar question on SO that is way simpler and easy to understand than mine!)

I actually found two ways to run my "scripts" from the web application project itself:

1) If Maven is installed on the prod server, you can run the scripts from the generated "target" folder, by setting the classpath as such when starting the script :

~ cd /path/to/project/target/classes

~ java -classpath ./:/path/to/project/target/myProject-0.0.1-SNAPSHOT/WEB-INF/lib/* com.path.to.scripts.ScriptToRun

2) It's also possible to run the script from the web server itself, where the .war was exploded. I tested it using Tomcat:

~ cd /path/to/Tomcat/webapps/ROOT/WEB-INF/classes

~ java -classpath ./:/path/to/Tomcat/webapps/ROOT/WEB-INF/lib/* com.path.to.scripts.ScriptToRun

Community
  • 1
  • 1
electrotype
  • 8,342
  • 11
  • 59
  • 96