Run Spring boot project from Maven or IDE - java

I've created Spring Boot based on Maven project as described in one of the official guides. I use IntelliJ.
Now I can run project in one of two ways:
Click on green arrow next to main method
Type mvn spring-boot:run in the command line
What's the difference between these two? Which one should I prefer and why? What does IntelliJ use internally to build the project?

The difference is that in case 1) IntelliJ starts the JVM and runs your app and in case 2) Maven does the same.
In development I would recommend to run the Spring Boot application via IntelliJ instead of Maven. If you run it with Maven, you bypass IntelliJ in a way so it will be less aware of what's going on and therefore less able to leverage it's assistance features. I'm not sure if the debugger will even work, if you start the app with mvn spring-boot:run.
IntelliJ internally uses the JDK that you configured for your project to compile your Java code and if it's a Maven project it also uses Maven to assemble the classes into an app. That last part of your question is extremely broad if one would attempt to answer it in detail. But in practice, when developing a Spring Boot app, you rarely need to worry about it anyway, to be honest.

Related

STS, running any of the Guide projects

I install Spring Toolsuite.
I go to New -> Import Getting Started project.
I choose the 'Building a RESTful Web Service' one. Download only the 'complete' project, select maven and not to open the site.
Now what on earth to I have to do to launch it? Right-click on the project, run as, run on server, shows 'The selection cannot be run on any server'.
I have gone through loads of posts trying to find out what else I can do, done maven clean, maven install, changed the JRE Environment, nothing works.
Running a maven clean or maven install will only update the dependencies of the project (for instance, bring in Spring and its child dependencies, loggers, etc. specified in the pom.xml file).
To run the project right click on the class with the #SpringBootApplication
annotation (it will likely be the one with a main method in it, that's the one you want!). Then select 'Run as->Java Application'.
First a disclaimer. This answer applies to most of the guides. But there may be some to which it doesn't apply, because there's such a diversity of guides available.
Assuming you are trying a 'typical' guide which does something in the context of a web-app... then read on.
The reason that you can not run a guide with "Run On Server" is because of spring-boot adheres to the slogan of "Make Jar not War". The guide sample code are not things you deploy on a server (i.e war) but are standalone Java apps (i.e. jar) which contain their own embedded servlet container (if they need one). That means running them is really quite simple. Just find the 'main' method/class in the guide and use Eclipse "Run As >> Java Application".
There's also a convenient alias in STS called "Run As >> Spring Boot App" which does pretty much the same thing but gives you a few extra bells and whistles in the launch configuration editor.
Also... you don't really have to go look for the main method yourself because the "Run As >> Spring Boot App" knows how to find it in most cases. So clicking project and "Run As >> Spring Boot App" should do the trick.

Is it advisable to have 2 web modules in a single IntelliJ project?

I created an IntelliJ (9) project, and it started off as a single maven pom.xml project.
This project is a spring mvc web application.
I now realize it will be better to actually have 2 spring mvc applications.
Since I use maven now to build and run the application (using the jetty plugin), should I create the 2nd spring mvc application in the same project?
If so, I guess I have to re-work the folders so it is like:
myapp-models/
myapp-spring1/
myapp-spring2/
So this way each spring app will have:
/src/java/main/webapp (or whatever it is, I used a simple web arch type)
Does this make sense?
Do I even bother setting things up in IntelliJ to build using the IDE? (using modules I think?)
Can I still build and run using intelliJ?
BTW, when browsing folders in IntelliJ, it is annoying to keep clicking through the first 3 folders since they are empty, is there a faster way?
This setup is just fine. You may choose which artifacts you want to deploy on Jetty. myapp-spring1 or myapp-spring2 or both. (Maybe artifacts were introduced after IntelliJ IDEA 9.)
BTW: I usually use "View as Package" in the Project tab. There you can select "Compact empty middle packages". At least in IntelliJ 10/11, but Im sure there is something similar in version 9.
You are correct in using multiple modules in Intellij. I create multi module projects all the time.

Why do I need Maven if I use Eclipse?

I have seen that if I right click on a project in Eclipse and choose to run it on a server, then I can see output which means the project is running.
If everything is working fine without Maven, what's the point of using it. How is it different than simply running it via eclipse?
Maven is a build tool (build manager, in fact), similar to ANT. The main job of any build tool is configure the project, compile using required projects and do the final packaging. A build script in your project gives a blue-print of project's deliverable structure. This frees you from any configurable dependencies on specific IDE like Eclipse. All you need to know is the standard command to perform the build and you can build your code almost anywhere.
Now, back to your question, why wouldn't do it in Eclipse?
For a simple project and small team Maven is an overkill. You can easily communicate the configuration, IDE to use, and instruct any special steps to be taken. In big projects, however, there exits lots of loosely coupled dependencies. To start with, there will be different settings for developer machine build, test build and production build. There are requirements to run automated test, integration tests, store the build package (artifact) to a commonly accessible repository, update versions of various modules.
Obviously, if all the steps mentioned above is done manually there are chances of missing a step. Moreover, the manual process is time consuming.
Ideally, you should prefer a tool which fits the best for you. If you think that you're able to achieve what you required without Maven, it makes sense to not to use Maven/build-tool just because everyone uses it.
It is suggested to study automated deployment, this will give you bigger picture on what all the stuffs that you can do with build tools. And if you do not feel that it adds any value to your current process, you probably don't need Maven or any other build tool right now.
Your question does not make much sense. Do you expect your users to access your application from eclipse? If so that is a very strange set up in my opinion.
Perhaps your question should be about how to build your project. Maven provides you a way to centralize dependency libraries across the enterprise. It lets you automate your build process (most likely in conjunction with a CI server like hudson, cruise control, etc). It lets you automate your unit testing. Maven makes the packaging of app very easy to do. A developer does not have to follow arcane set of steps to package an application. You add the right plugin and maven takes care of it as part of the build life cycle. All of this magic can happen because of the principle of convention over configuration. There are many more benefits, I just named a few.
Maven is not replacing how you run the app, rather how you package the app, automate that process, and manage the dependencies of your app.
Some links on why someone should use maven:-
Why maven ? What are the benefits?
why I use Maven
Why you should use Maven
Use Maven

Maven + Tomcat acceleration

I am writing a web application with Maven in the Eclipse IDE, and use Tomcat servlet container.
So, I run Maven like this: mvn clean compile. It is reasonable that after this operation I must re-run Tomcat so it can reinitialize the context (Sysdeo Tomcat launcher helps a lot).
The problem is Maven execution and subsequent Tomcat re-running takes noticable amount of time (like 10+ seconds for Maven and 20+ sec. for Tomcat, because of logging, O/R mappings, etc.) every time I do it.
Is there any automated and more faster solution for these operations? As I see it, a way better solution can be moving re-compiled classes only to the target dir.
Is there any automated and more faster solution for these operations? As I see it, a way better solution can be moving re-compiled classes only to the target dir.
Well, the question is why do you run clean each time? Doing incremental compilation would already speed up things a lot.
Update: I agree with #Carl about Eclipse WTP that provides very good support of Tomcat (I don't really see the added value of the Sysdeo plugin nowadays). Using Eclipse WTP for development and running Maven before to commit the changes to check that you didn't break the continuous build is a very typical workflow. And both the maven-eclipse-plugin and m2eclipse (the two alternatives for Maven and Eclipse integration) support the WTP i.e. can get your project recognized as a dynamic project than can be Run on a Server.
You may want to have a look at JRebel. It reloads your classes in a running tomcat, so your changes are near instantaneous. I haven't used it much, but it appears to solicit good comments.
Maven does two things: Dependency handling and build management. I usually find Maven's dependency management a big time-wasting annoyance that I usually don't need, so I do my build management with ant.
At the price of a hand-tuned build file, ant gives you very good control over which files go where when. If you copy newly compiled classes to your WEB-INF/classes directory and touch web.xml to trigger a reload, you don't have to stop and restart Tomcat. This brings my compile/reload time down to around one second.
This is how I prefer to work. Some Maven fans will disagree violently.
EDIT: That said, there's another method that allows me to skirt the build issue completely: I develop in Eclipse using the WTP functionality that's included with the Java EE developer's edition. When I make a code change, I simply hit Ctrl-S to save the changed file and Eclipse automatically copies the newly compiled class into the running Tomcat, so I can then immediately refresh my browser and see the newly changed Web app running. Thanks to Eclipse's incremental compilation, this method probably is probably unbeatable in terms of edit/run cycle time. Of course if you really need Maven then this is not an alternative.
There is Maven tomcat plugin can help you, you just execute "mvn tomcat:redeploy", and maven compile the source, package it and deploy it to your configured tomcat, see tomcat plugin for more information.
Eventually, I've solved that by using Eclipse feature called «Build Automatically» (Project → Build Automatically checkbox).
Every time you save a resource, Eclipse compiles it and moves .class file to the output folder.

Spring Hello World setup and code in eclipse

I have setup Spring / WTP in eclipse as well as successfully started a Tomcat 6 server within eclipse. I'm at the point where I can create a new Spring project and add source files as necessary.
Could somebody please describe (or point me to) how I can setup of some sort of 'Hello World' test and how to run it? I have experience developing in Java SE but am trying to learn Java EE / Spring.
Start with "Spring MVC Step by Step".
I know this does not answer your question directly, but you may want to consider generating a base project using Maven and then generating an Eclipse project from that.
If you decide to go down the Maven path, the process would be:
mvn archetype:generate -B -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-spring-archetype -DarchetypeVersion=2.1.0-M1 -DgroupId=com.mycompany -DartifactId=myproject
Details: AppFuse
Once you run the above command, it will set up a complete maven project for you with a Spring MVC project stubbed out.
The next step is to simply run: mvn eclipse:eclipse to generate the project.
Details: Maven Eclipse Plugin
Once you get the code generated and the Eclipse project set up, you can read on the AppFuse Quickstart page how to run the application locally, how to debug it, and go from there.
If you're looking at using Spring in a Web-app, then perhaps this previous answer I wrote might help?
For building Spring apps, SpringSource provides a build of Eclipse 3.5 called SpringSource Tool Suite. It includes a bunch of plugins that you can download independently, but this comes with them pre-integrated. It also includes a lot of guides, documentation, wizards, and so on, which might be good to get you started.
You may find interesting the Spring Roo project which creates a Spring MVC based project directly on the IDE or via the command line.
You might want to see the ten minute video to see how it can get you up and running in no time and it also includes other features which can be not that easy to set up like security and internationalization.

Categories

Resources