Maven $JAVA_HOME is not defined correctly on Mac OS

This article shows how to fix the Maven error JAVA_HOME is not defined correctly. Terminal $ mvn -version Error: JAVA_HOME is not defined correctly. We cannot execute /usr/libexec/java_home/bin/java Further Reading How to set $JAVA_HOME environment variable on macOS 1. $JAVA_HOME and macOS 10.15 Catalina, macOS 11 Big Sur On macOS 10.15 Catalina and later, the …

Read more

How to create a manifest file with Maven

This tutorial will show you how to use the maven-jar-plugin to create a manifest file, and package / add it into the final jar file. The manifest file is normally used to define following tasks : Define the entry point of the Application, make the Jar executable. Add project dependency classpath. When you run the …

Read more

How to create a jar file with Maven

In this tutorial, we will show you how to use Maven build tool, to create a single executable Jar, and how to deal with the project’s dependencies. Tools used : Maven 3.1.1 JDK 1.7 log4j 1.2.17 Joda-time 2.5 Eclipse 4.3 1. Create a simple Java project Create a Java project from the Maven quick start …

Read more

How to create user defined properties in Maven

Custom properties or variables are useful to keep your Maven pom.xml file more easy to read and maintain. File : pom.xml <project> … <properties> <my.plugin.version>1.5</my.plugin.version> </properties> … </project> In above pom.xml, you can refer “my.plugin.version” via code ${my.plugin.version}. Example 1 A classic use case is used to define a jar or plugin version. <properties> <spring.version>3.1.2.RELEASE</spring.version> …

Read more

Eclipse code assist in Maven pom.xml

By default, code assist in editing a pom.xml is NOT supported in Eclipse IDE. It make Maven project hard to maintain, and who will remember who all those tags? Solution You need a pom.xml Maven editor, which is available in m2eclipse plugin. In Eclipse, menu, select “Help” -> “Install New Software”, and put following m2eclipse …

Read more

Maven M2_REPO is non modifiable

Problem Forgot what i did, may be installed m2eclipse plguin, it made the M2_REPO classpath variable is unable to edit. Select Windows -> Preferences -> Java -> Build Path -> Classpath Variables. Eclipse come with embedded Maven and have this M2_REPO classpath variable default non modifiable also. Figure – M2_REPO is non modifiable Solution To …

Read more

Maven 3 need to know the plugin version

In Maven 2, if you didn’t specify the version for each plugins that used in pom.xml, it will pick the latest plugin version automatically, which is very convenient. However, in Maven 3, if you didn’t explicitly specify the plugin version, it will prompt you warning message. Read this “Maven 3 compatibility” for detail. For example, …

Read more

Get HttpServletRequest via Maven repository

Problem Using Maven build tool, which jar should include to use HttpServletRequest or HttpServletResponse? Solution To use HttpServletRequest or HttpServletResponse in a development environment, include servlet-api. jar in your Maven pom.xml file. pom.xml <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> Put scope as provided, because most j2ee containers have this jar. You need this for complication …

Read more

Maven 3 + Hibernate 3.6 + Oracle 11g Example (Annotation)

This tutorial will reuse and modify the previous Hibernate3.6 XML mapping tutorial, but replace the Hibernate mapping file (hbm) with Hibernate / JPA Annotation code. Technologies in this article : Maven 3.0.3 JDK 1.6.0_13 Hibernate 3.6.3.final Oracle 11g 1. pom.xml No change in pom.xml file, all previous Hibernate3.6 XML mapping tutorial dependency can be reused. …

Read more

Maven 3 + Hibernate 3.6 + Oracle 11g Example (XML Mapping)

In this article, we show you how to integrate Maven3, Hibernate3.6 and Oracle11g together. In the end of this article, you will create a Java project with Maven, and insert a record into Oracle database via Hibernate framework. Tools & technologies used in this article : Maven 3.0.3 JDK 1.6.0_13 Hibernate 3.6.3.final Oracle 11g 1. …

Read more

How to add Oracle JDBC driver in your Maven local repository

Here’s a simple guide to show you how to add an Oracle JDBC driver into your Maven local repository, and also how to reference it in pom.xml Tested with Oracle database 19c and Java 8 Note Due to Oracle license restrictions, the Oracle JDBC driver is not available in the public Maven repository. To use …

Read more

Generate source code jar for Maven based project

The “maven-source” plugin is used to pack your source code and deploy along with your project. This is extremely useful, for developers who use your deployed project and also want to attach your source code for debugging. 1. Maven Source Plugin Add maven-source-plugin in your pom.xml file. pom.xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <executions> <execution> …

Read more

Generate javadoc jar for Maven based project

The “maven-javadoc” plugin uses “JDK\bin\javadoc.exe” command to generate javadocs, pack in jar file and deploy along with your project. 1. Maven JavaDoc Plugin Add “maven-javadoc” plugin in your “pom.xml” file. File : pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mkyong</groupId> <artifactId>mkyongcore</artifactId> <packaging>jar</packaging> <version>1.0</version> <name>mkyongcore project</name> <url>http://maven.apache.org</url> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <executions> <execution> <id>attach-javadocs</id> <goals> …

Read more

Maven + WebDAV – Embedded error: Failed to transfer file: … Return code is: 405

Problem A pom.xml file, when “mvn site:deploy” is issued, the site doesn’t deploy to the defined server and hits the HTTP error code : 405. File : pom.xml <project …> <build> <extensions> <extension> <groupId>org.apache.maven.wagon</groupId> <artifactId>wagon-webdav-jackrabbit</artifactId> <version>1.0-beta-7</version> </extension> </extensions> </build> <distributionManagement> <site> <id>sitedeployment</id> <url>dav:http://127.0.0.1/upload-sites/</url> </site> </distributionManagement> </project> See full error message : D:\workspace-new\mkyong-core>mvn site:deploy [INFO] Scanning …

Read more

How to deploy site with “mvn site-deploy” – WebDAV example

Here’s a guide to show you how to use “mvn site:deploy” to deploy your generated documentation site to server automatically, via WebDAV mechanism. P.S In this article, we are using Apache server 2.x with WebDAV enabled. 1. Enabled WebDAV See this guide to learn how to enable WebDAV access on Apache server 2.x. 2. Configure …

Read more

Integrate Maven with Eclipse via External Tool Configuration

During Eclipse development, it’s not practical to go outside of the Eclipse’s environment and execute Maven command in external console. In this article, we show you how to integrate Maven command with Eclipse IDE, via Eclipse’s “External Tool Configuration“. Normally, this external tool is used to run external command within Eclipse environment, and it works …

Read more

How to deploy Maven based war file to Tomcat

In this tutorial, we will show you how to use Maven-Tomcat plugin to package and deploy a WAR file to Tomcat, both in Tomcat 6 and 7. Libraries used : Maven 3 Tomcat 6.0.37 Tomcat 7.0.53 Tomcat 7 Deploy URL = http://localhost:8080/manager/text Command = mvn tomcat7:deploy Tomcat 6 Deploy URL = http://localhost:8080/manager/ Command = mvn …

Read more

How to generate a documentation site for your Maven based project

In Maven, you can use “mvn site” to generate a documentation site for your project information. mvn site The generated site is under your project “target/site” folder. mvn site example See a list of files generated via “mvn site” command. A sample of documentation page. Note Personally, i do not like this feature much, because …

Read more

How to install your project into Maven local repository

In Maven, you can use “mvn install” to package your project and deploy to local repository automatically, so that other developers can use it. mvn install Note When “install” phase is executed, all above phases “validate“, “compile“, “test“, “package“, “integration-test“, “verify” phase , including the current “install” phase will be executed orderly. Refer to this …

Read more

How to clean project with Maven

In Maven based project, many cached output existed in your “target” folder. When you want to build your project for deployment, you have to make sure clean all the cached output so that you are always get the latest for deployment. To clean your project cached output, issue this command : mvn clean See output… …

Read more

How to build project with Maven

To build a Maven based project, open your console, change to your project folder where pom.xml file is placed, and issue this command : mvn package This will execute the Maven “package” phase. Maven build lifecycle Maven is run by phases, read this default Maven build lifecycle article for more detail. So, when the “package” …

Read more

How to display Maven plugin goals and parameters

How do you know what is the available goals of a maven plugin, for example maven-eclipse plugin – mvn eclipse:goals?. In this article, we will show you how to display the entire goals of a Maven plugin. Plugin Documentation The best solution is Googling and visit the plugin documentation, for maven-eclipse, visit this maven-eclipse plugin …

Read more

Maven : generics are not supported in -source 1.3

Problem Maven as project build tool. Using Java generic function, when build with Maven, get this error message generics are not supported in -source 1.3 (use -source 5 or higher to enable generics) Solution You need to tell Maven to use JDK 1.5 to compile your source code explicitly. Declare Maven compiler plugin (maven-compiler-plugin) in …

Read more

Connect to Oracle DB via JDBC driver

A JDBC example to show you how to connect to a Oracle database with a JDBC driver. Tested with: Java 8 Oracle database 19c Oracle JDBC driver for Java 8, ojdbc8.jar 1. Download Oracle JDBC Driver Visit Oracle database website and download the Oracle JDBC Driver. 2. JDBC Connection Note Find your Oracle SID in …

Read more

Eclipse : Web Deployment Assembly & Maven dependencies issue

Problem In Eclipse 3.5 or early version, in order to deployed the Maven dependencies to the correct “/WEB-INF/lib” folder, you have to configure the dependencies via “Java EE Module Dependencies”, and the updated “.classpath” file will look like following : File : “.classpath”, by Java EE Module Dependencies… … <classpathentry kind="var" path="M2_REPO/javax/servlet/jsp/jsp-api/2.1/jsp-api-2.1.jar" sourcepath="M2_REPO/javax/servlet/jsp/jsp-api/2.1/jsp-api-2.1-sources.jar"> <attributes> <attribute …

Read more

How to add M2_REPO classpath variable to Eclipse IDE

Normally, when you use Maven command mvn eclipse:eclipse to convert existing Java project to support Eclipse project, Maven will create the entire dependency classpath by using the M2_REPO variable, which is not defined in Eclipse by default. Nothing special, M2_REPO is just a normal “classpath variable” in Eclipse to find your local Maven repository. Here, …

Read more

Download standard.jar (taglib) from Maven

The standard.jar (taglib) library is used to enable the JSTL expression language in JSP page, and it’s always used together with the jstl.jar together. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> …. </html> To download both standard.jar and jstl.jar from Maven, just put the following Maven coordinate in the pom.xml file. <!– standard.jar –> <dependency> <groupId>taglibs</groupId> …

Read more

Maven dependency libraries not deploy in Eclipse IDE

Problem By default, while starting the Tomcat server instance in Eclipse, the project’s dependency libraries will not deploy to the Eclipse’s Tomcat plugin library folder ‘WEB-INF/lib’ correctly. See this “.classpath” file, that is generated by Maven “mvn eclipse:eclipse” command. <classpath> <classpathentry kind="src" path="src/main/java" including="**/*.java"/> <classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/> <classpathentry kind="output" path="target/classes"/> <classpathentry kind="var" path="M2_REPO/org/apache/struts/struts-core/1.3.10/struts-core-1.3.10.jar" /> …

Read more

Maven + (Spring + Hibernate) Annotation + MySql Example

Download It – Spring-Hibernate-Annotation-Example.zip In last tutorial, you use Maven to create a simple Java project structure, and demonstrate how to use Hibernate in Spring framework to do the data manipulation works(insert, select, update and delete) in MySQL database. In this tutorial, you will learn how to do the same thing in Spring and Hibernate …

Read more

Maven – How to download JavaMail API

JavaMail is now available in Maven Central repository. pom.xml <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> If you want to use the core JavaMail API to send email, you have to get the JavaMail library – mail.jar and activation.jar. Unfortunately, those libraries are not available in the Maven central repository, you have to download it from java.Net …

Read more

Maven + Spring + Hibernate + MySql Example

This example will use Maven to create a simple Java project structure, and demonstrate how to use Hibernate in Spring framework to do the data manipulation works(insert, select, update and delete) in MySQL database. Final project structure Your final project file structure should look exactly like following, if you get lost in the folder structure …

Read more

How to download J2EE API (javaee.jar) from Maven

This below java.net javaee.jar solution is only contains the J2ee APIs, it does not contain any method bodies. It’s fine for compilation, but not for run or deploy your application, because it will caused ” Absent Code attribute in method that is not native or abstract in class” or other method not found errors. Due …

Read more