Converting a Netbeans project to a Maven enabled project

NetbeansMaven

Netbeans Problem Overview


How can I transition a Netbeans generated project into accepting a Maven configuration? There are options to create Maven based projects, but there is nothing (that I've found so far) to add Maven dependencies to existing projects.

Netbeans Solutions


Solution 1 - Netbeans

You need to create a separate Maven Project. Then, you can copy the code from your other project to the Maven project. This can be done from the Projects windows in NetBeans.

Just select the code files/packages in the tree, right-click for copy, then paste them in the Source Packages of your new Maven project.

Next, open the files which Maven won't compile because they miss dependencies. The yellow bulb on the left of the problematic line will give you options to search for missing dependencies and add them to your project. You need to be online to perform searches.

You can also add maven dependencies manually in your new Maven project by right-clicking the dependencies folder in the Projects windows.

Solution 2 - Netbeans

If you are familial with maven, then you can always configure maven even in the later, however it is not recommended.

the only reason behind people(including me ;) ) recommend to create a new maven project, is Maven has it's own directory structure. And that is standard. now if you want to enable maven for your project at a later stage, than you can configure the things in pom.xml, i.e. your source directory, target directory and web app directory(if applicable)

I had a large project in SVN and was disallowed to create a new project. I did not want to support lib management and so I configured maven according to my directory structure.

here's the part of my pom.xml

	<build>
	<sourceDirectory>src</sourceDirectory>
	<testSourceDirectory>testpackages</testSourceDirectory>
	<testOutputDirectory>target/test-classes</testOutputDirectory>
	<plugins>
		<plugin>
			<version>2.3.2</version>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.6</source>
				<target>1.6</target>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-war-plugin</artifactId>
			<version>2.1.1</version>
			<configuration>
				<failOnMissingWebXml>false</failOnMissingWebXml>
				<webResources>
					<resource>
						<!-- this is relative to the pom.xml directory -->
						<directory>web-root</directory>
					</resource>
				</webResources>
			</configuration>
		</plugin>

Solution 3 - Netbeans

I follow these steps for my desktop Java application (backup your project before doing this):

  1. Open your project in Eclipse (by going to new project and browse to your project). Importing the project will not work.

  2. Enable maven for this project

  3. Add dependency library

  4. Close your project

  5. Delete NBProject folder and Build.xml from the project location (otherwise NetBeans will not able to recognize it as maven project).

  6. Open this project in NetBeans

Solution 4 - Netbeans

I discovered by accident, the reason why Netbeans 8.2. opens projects as Netbeans projects instead of Maven projects; after you have deleted the netbeans and ant specific files then replaced them with the correctly formed pom.

Netbeans seems to cache the project types for already opened projects, this can be resolved by deleting said cache.

Windows its located here:

C:\Users\${username}\AppData\Local\NetBeans\Cache

Linux here:

~/.cache/netbeans/${netbeans_version}/index/

Mac here:

~/Library/Caches/NetBeans/${netbeans_version}/

Solution 5 - Netbeans

Improving @JVerstry Answer.. I add Another solution unexplained here in steps.

Create a new Maven project in Netbeans. Then copy paste your source code in your maven folder, it can be done in Netbeans IDE Projects View Itself. After this follow these steps.

  1. Add SPRING MVC Dependencies.
  2. Add Spring MVC Version to your properties section (Dependent on point 1)
  3. Configure dispatcher-servlet.xml and web.xml in your Spring Folder.
  4. Rest is to do some matching and configuring your main Controller & JSP / HTML-Page.,
  5. check your home page by running.

Point 1:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>${spring.version}</version>
</dependency>

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${spring.version}</version>
</dependency>

Point 2: and add spring.version to your properties section

<properties>
    <spring.version>4.0.2.RELEASE</spring.version>
</properties>

Point 3: Under WEB-INF folder, create a file named dispatcher-servlet.xml. Open the file and copy the following code.

<?xml version="1.0" encoding="UTF-8"?>
   <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<mvc:annotation-driven />
<context:component-scan base-package="com.youbequityweb.controllers" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>
</beans>

The declares support annotation driven mvc such using @Controller, @Service, @Component.

The means to scan classes from this base package to determine all bean classes.

The view resolver specifies the locations of our views(jsp) and the extension. In your web.xml, add the spring configurations inside web-app section.

<!--Spring Config-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>    <listenerclass>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-  class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Point 4: This is tricky, now Link your existing main controller to the base package defined in dispatcher-servlet.xml. e.g: HomeController.java shown below.

package com.youbequityweb.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {

@RequestMapping(value="/home", method = RequestMethod.GET)
    public String viewHome(){
    return "home";
    }
}

Point 5: In NetBeans, now right click, choose clean and build, get away if any warnings and conflicts and than run your spring mvc application for results.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmonksyView Question on Stackoverflow
Solution 1 - NetbeansJérôme VerstryngeView Answer on Stackoverflow
Solution 2 - NetbeansTejasView Answer on Stackoverflow
Solution 3 - NetbeansHarsh kurraView Answer on Stackoverflow
Solution 4 - NetbeansKatView Answer on Stackoverflow
Solution 5 - NetbeansNadZView Answer on Stackoverflow