How do I execute a script (executable class) located inside a Spring MVC project?

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

The "scripts" will load some context files from the Spring MVC application and access the properties files from the web application. This way they will be able to use the same database connection settings as the application, etc.

My question is, how do I run these scripts?

Locally I am using Eclipse and run these "scripts" with Run as / Java Application. But I want to be able to run them on my prod server too, where I cannot use eclipse to run them. What should I do to get them running there?

Should I export my app in a different format than .war? Do I have to create an additional .jar from the project to be able to run scripts with "java -jar"?

Should I create a separate project for these scenarios? But then, will the scripts be able to load the required contexts from a Spring MVC application located in another project?

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

UPDATE . One way to see that I could achieve what I want is to force the web application to expose web services as a door to perform tasks. Then I could call those using wget or something on the command line. But I would rather be able to access the classes directly, without having to create web services.

+3


source to share


2 answers


(First, I found a similar question on SO that's simpler and clearer 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 scripts from the generated "target" folder by setting the classpath as such when running 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 is also possible to run a script from the webserver itself where the .war was blown up. I tested it with 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

0


source


What is the purpose of your "java scripts"



  • Do something periodically in the bakcground. You can use the Quartz framework to automatically start the job. see Quartz scheduler
  • To make sure something is working correctly. I used the main () method in my class to make sure something was working correctly before I learned about junit. See junit
0


source







All Articles