How to make Visual Studio recognize Spring Boot packages and get IntelliSense? - java

I have created a Spring Boot application using Spring Intializr extension of VS Code.
Configuration, Build Tool : maven, Language : Java, groupId : com.example, artifactId : springdemo, Spring boot version : 2.0.3, I have selected 2 dependencies which are Spring MVC and Spring DevTools Core from the dropdown.
Here is the screenshot Screenshot of the selected dependencies
After importing it and opening the folder structure Project Structure after vs code imports the maven project and opens up the folder
After this I ran a command
chmod 777 mvnw
to give permission to the gradle wrapper and after that
./mvnw clean install
This seems to work fine logs after the ./mvnw clean install command
This is my pom.xml
My pom.xml contains
Here is the difference : My VS Code project does not contain the maven dependencies folder as in case of eclipse and even after install all the packages vs code does not seem to recognize the packages and shows error
errors in vs code
This is how my eclipse project structure look like eclipse project structure contains the maven dependencies
The final result is a running project. The project runs even from vs code by ./mvnw spring-boot:run
but I am unable to get any kind of IntelliSense from VS Code and although eclipse works but I need to use VS Code. Please help.

Related

Initializing and running springboot project using maven in Intellij?

I get a spring boot with maven project from github, which works well in my teammates computers. I am using Intellij idea and I am confused about how to run it in my computer. Should I initialize a project or can I just use the project directly by open folder?
In edit configuration part, I choose maven and the directory containing pom.xml. In that case it gives noGoalsSpecified error. So should I include a goal in command line part of edit configuration. In short, how to run the project?

How to export a javafx maven project with jfoenix components using the javafx-maven-plugin

I have been working on a javaFx project with maven. exporting this project to an executable file seems to very hard.
though my research i found that javafx-maven-plugin is the tool for that and with the help of my last question i figured out the configuration to run the project using the plugin.
How to run a maven java fx project that includes jfoenix using javafx-maven-plugin
my assumption was that if i could get the project to run, then exporting using the same tool can be easy. but i have been try all possible changes and nothing works.
export the project using
mvn javafx:jlink
run the project by using the following cmd at the bin
java -m "moduleName"/"reference of the the main class"
all the technical information are in the linked question. how can i export this project properly.

How to create a Java Spring WebApp Folder Structure using Gradle

I am making a Java Web App project using Gradle. Now, for building a Gradle web app plenty of resources are available over the net.
But, my issue is a bit different. Suppose, I want to initiate a Gradle based project and make a web app suitable project folder structure just like we do in maven. Is there any specific way to create so in Gradle. I have found something about templates. Is there any way to do it or do I have to create a folder structure manually and then attach to Gradle so that it can build a war out it properly.
You can use maven to create the project and then create gradle build from it:
mvn archetype:generate -DgroupId=com.example -DartifactId=java-web-project -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
Then:
gradle init --type pom
You didn't mention your IDE, but I will answer for IntelliJ IDEA and for Eclipse the steps are similar.
Go to File --> New --> Project, choose Gradle from the left menu and choose both Java and Web options (see image below).

Intellij idea cant access to starter class of Spring Boot

my class of spring boot start located in an external jar.This external jar is a dependency for this project.When I try to create a configuration of Spring Boot in IntelliJ, it saying that it cannot access this class.
I added in pom.xml a tag of the main class, and running it through plugin of maven Spring Boot, and everything work, but its annoying, because every change I need to run install.When I open the jar via dependency window in IntelliJ, I can see this class and have an option of run , and if you press run IntelliJ says cannot find spring boot class to run.
Check: Project Settings | Maven | Importing | Import Maven projects automatically.
Plus, in the maven tab, you can add external projects as direct dependency (the green '+' button). There you have to select your project that you also added as pom.xml
Then you only have to run mvn install on the external project if you want to build the jar/war for your impl project. But within the IDE not need to execute installs.
In new version you have check box of include provided dependices.Check it and will work

Java Dynamic Web project with Maven and Eclipse

I have several questions about creating a Java Web application with Maven and Eclipse:
How do I create a Java web project with servlets, jsp, and other classes with Maven?
It creates a simple directory structure, src->main->java. Where and how do I put the web-inf folder?
Do I need to add the jdbc-drivers manually to the folder inside the web-inf/lib, or is it ok just to point out the dependency?
Is there a way to test the servlets with junit?
Wow that's a lot of questions at once. I admit that setting up a webapp project with Maven and Eclipse can be tricky, so I'll try to answer them all.
Creating a Web application project with Maven
How do I create a java web project with servlets jsp and other classes with maven? It creates a simple directory structure, src->main->java.
When you are creating a Java web project, the final product should be a WAR or EAR file. WAR and EAR files are JAR files with a specific structure that can be deployed in an application server or servlet container.
As mentioned, the easiest way to set up a Maven project for web applications is to use archetypes:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp
If we create a project with this archetype then a simple directory structure and pom.xml are generated. This project follows the standard Maven directory layout you mention, with /src/main/java/, /src/test/java, etc. Maven generates the WAR file from this structure with the war:war goal.
Note that this archetype is for a very simple (outdated) web application, but it's the best available starting point. You probably want to discard the web.xml file and create a new one that supports Servlet 3.0.
WEB-INF location
Where and how do I put the web-inf folder?
By default, Maven expects resources that should go in the root of the WAR file -- such as images, html pages and the WEB-INF directory -- to reside in /src/main/webapp/. So the WEB-INF folder should be located at /src/main/webapp/WEB-INF/. If you use the maven-archetype-webapp this directory is automatically created, along with a sample web.xml file.
Eclipse integration
You mentioned Eclipse in the question title, and it is indeed possible to develop Mavenized web applications in Eclipse with the m2eclipse plugin. Eclipse has good support for Web applications through WTP (Web Tools Platform).
Although many guides on the internet (wrongly) recommend it, you should not use the mvn eclipse:eclipse command to create the Eclipse project. This plugin can only generate WTP projects for very old Eclipse versions (WTP 2.0 is the maximum). Instead, use the m2eclipse plugin as described here.
Dependencies
Do I need to add the jdbc-drivers manually to the folder inside the web-inf/lib, or is it ok just to point out the dependency?
There is no need to do this manually, since one of the key strengths of Maven is dependency management. If you add a dependency in the pom.xml with a scope of compile or runtime, the JAR file will be automatically included in the WEB-INF/lib/ directory of the WAR file. For example to add the Postgresql JDBC driver dependency, add this to your pom.xml:
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
Since the scope is unspecified Maven will assume it is in the the default scope compile. The result is that Maven will include WEB-INF/lib/postgresql-9.1-901.jdbc4.jar in the WAR file.
Testing
Is there a way to test the servlets with junit?
This question has been asked (and answered) on Stackoverflow:
Unit testing a Java servlet
Unit testing servlets
References
Hello World with JSF 2.0, Glassfish 3, Maven, SVN and Eclipse.
You should create a project based on the webapp Maven archetype, not the default one you're using.
I'm using SpringSource Tool Suite, which, for this exercise, is the same as Eclipse with m2e. Create a new Maven project and make sure you select the following archetype:
The Maven the command-line way of doing this is:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
This archetype will put the WEB-INF folder in the correct location (under src/main/webapp).
You can find more information at http://maven.apache.org/guides/mini/guide-webapp.html
Just create a normal "Dynamic Web Project". Then right click the project name and select Configure > Convert To Maven Project. It can take up to a minute to complete the conversion, so be patient. See the following article:
http://aykutakin.wordpress.com/2013/12/04/create-eclipse-dynamic-web-project-with-maven-2/
If that doesn't work, try this:
http://crunchify.com/how-to-create-dynamic-web-project-using-maven-in-eclipse/
Step 1: create your web app folder.
Step 2: Move to that folder in command prompt.
Step 3: use following command:
mvn archetype:generate -DgroupId=com.helloworld -DartifactId=HelloWorldDemo -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
DarchetypeArtifactId=maven-archetype-webapp will create a maven web project.
Step 4: Now on command prompt go inside the project folder and use this command:
mvn eclipse:eclipse -Dwtpversion=2.0 to make your project a dynamic web project for eclipse, now import this newly created project as an "Ëxisting project"
Use m2eclipse plugin of eclipse to enable MAVEN in eclipe.
Web.xml will be at \src\main\webapp\WEB-INF

Categories