Installing External Jars to Local Maven Repository

Reading Time: 1 minute

Central Maven repository has a large variety of libraries https://mvnrepository.com however there are times that we may be in need of installing such 3rd party jars that are not offered in the central in mavenized projects. This command will allow you to install such jars to your local M2 folder. You need to make sure that if multiple developers are working on the project that experiences this situation, the participants must also carry out the same procedure.

The Command

[bash lang=”bash” smarttabs=”true” tabsize=”4″ wraplines=”true”]mvn install:install-file -Dfile={FILELOCATION}
-DgroupId=GROUPID/KNOWNASPACKAGENAME -DartifactId=ARTIFACTID
-Dversion=VERSION -Dpackaging=PACKAGINGTYLE[/bash]

Example Command

[bash smarttabs=”true” tabsize=”4″ wraplines=”true”]mvn install:install-file -Dfile=C:\myjar1-0.jar
-DgroupId=com.tugrulaslan -DartifactId=MyJar
-Dversion=1.0 -Dpackaging=jar[/bash]

Usage in pom.xml

<dependency>
<groupId>com.tugrulaslan</groupId>
<artifactId>MyJar</artifactId>
<version>1.0</version>
</dependency>

External Links
https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

Passing the selected maven profile/value to java

Reading Time: 1 minute

in this post we will pass the selected env value to the java file.  when you start the project as follows:

-Denv=asseco-test

pom.xml

<profiles>
 		<profile> 
 			<id>asseco-test</id> 
 			<activation> 
 				<activeByDefault>true</activeByDefault> 
 				<property> 
 					<name>asseco-env</name> 
 					<value>asseco-test</value> 
 				</property> 
 			</activation> 
 			<properties> 
 				<profile-id>asseco-test</profile-id> 
 			</properties> 
 		</profile> 
 		<profile> 
 			<id>asseco-prod</id> 
 			<activation> 
 				<activeByDefault>false</activeByDefault> 
 				<property> 
 					<name>asseco-env</name> 
 					<value>asseco-prod</value> 
 				</property> 
 			</activation> 
 			<properties> 
 				<profile-id>asseco-prod</profile-id> 
 			</properties> 
 		</profile>
</profiles>

java file

private Properties props = new Properties();
String selectedPaymentProfile = System.getProperty("env");
		final String fileName = selectedPaymentProfile.concat(".properties");
		try {
			props.load(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(paymentProperties)));
			
			URI = props.getProperty("URI");
			
		} catch (FileNotFoundException e) {
			logger.error("Exception in providerDetails: " + e);
		} catch (IOException e) {
			logger.error("Exception in providerDetails: " + e);
		}

 

 

One jar with all dependencies

Reading Time: 1 minute

The configuration is below when you issue this command “mvn clean package assembly:single” then you will get a jar file containing all the sources and if  you check the me MANIFEST.MF file and you will see the line “Main-Class: com.tugrulaslan.App” the value we set below in the 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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>de.goeuro</groupId>
	<artifactId>GoEuroApp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>GoEuroApp</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<jdk.version>1.7</jdk.version>
		<maven-compiler.version>3.3</maven-compiler.version>
		<maven-jar-plugin.version>2.6</maven-jar-plugin.version>
		<maven-assembly-plugin.version>2.5.5</maven-assembly-plugin.version>
	</properties>

	<dependencies>
		...
		</dependency>
	</dependencies>
	<build>
		<finalName>${project.artifactId}</finalName>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<version>${maven-compiler.version}</version>
					<configuration>
						<source>${jdk.version}</source>
						<target>${jdk.version}</target>
					</configuration>
				</plugin>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-assembly-plugin</artifactId>
					<version>${maven-assembly-plugin.version}</version>
					<configuration>
						<finalName>${project.artifactId}</finalName>
						<appendAssemblyId>false</appendAssemblyId>
						<archive>
							<manifest>
								<mainClass>com.tugrulaslan.App</mainClass>
							</manifest>
						</archive>
						<descriptorRefs>
							<descriptorRef>jar-with-dependencies</descriptorRef>
						</descriptorRefs>
					</configuration>
					<executions>
						<execution>
							<phase>package</phase>
							<goals>
								<goal>single</goal>
							</goals>
						</execution>
					</executions>
				</plugin>
			</plugins>
		</pluginManagement>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>${maven-jar-plugin.version}</version>
			</plugin>
		</plugins>
	</build>
</project>

Excluding jar/subdependency from a main dependency from a maven project

Reading Time: 1 minute

In my project at work, the front end java framework ZKoss I use at work, pulls servlet api jar on its own since its dependency is required for the jar, it was mismatching one i was using. So I had to exclude the jar from zkoss dependency

the below is the ZK’s required jar and I am excluding servlet-api from the dependency and pulling the servlet-api upon my request.

<!-- ZK Spring dependencies servlet-api being excluded-->
		<dependency>
			<groupId>org.zkoss.zk</groupId>
			<artifactId>zkspring-core</artifactId>
			<version>${zk-spring.version}</version>
			<exclusions>
				<exclusion>
					<groupId>javax.servlet</groupId>
					<artifactId>servlet-api</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

<!--This is the one I am pulling from maven's repo -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>${servlet-api.version}</version>
		</dependency>

Adding external jars to the local maven repository

Reading Time: 1 minute

At work I faced a situation that I had to add external jar libraries into my maven project, since most of required Java packages offered by Maven the automation, I have come across with the situation that Business objects external jars needed to be imported into the project. So I have done a small research regarding the issue, here is the outcome. First of all keep all he required in an easy location. I put the files in a folder called “libs” located right underneath C root drive the command below will do the magic

C:\workspace\CMC_REPORTING>mvn install:install-file -Dfile={FILELOCATION} -DgroupId={GROUP/DOMAINNAME} -DartifactId={FILENAME} -Dversion={VERSION} -Dpackagi
ng=jar

This command is going to add the required file to your local maven repo and you will get to use it, sample output after you carry out this command is:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\lib>mvn install:install-file -Dfile=fedmaster.jar -DgroupId=com.sap -Dartifac
tId=fedmaster -Dversion=1.0 -Dpackaging=jar
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ standalone-pom
---
[INFO] Installing C:\lib\fedmaster.jar to C:\Users\akn522a\.m2\repository\com\sa
p\fedmaster\1.0\fedmaster-1.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.365s
[INFO] Finished at: Mon Jan 20 09:21:16 EET 2014
[INFO] Final Memory: 2M/15M
[INFO] ------------------------------------------------------------------------
C:\lib>

After the screen the necessary .jar file has been successfully imported into our maven local repo. There is one point left needs to be done, adding the imported jar in our pom.xml file. As usual add the line to your pom.xml

<dependency>
		<groupId>com.sap</groupId>
		<artifactId>fedmaster</artifactId>
		<version>1.0</version>
</dependency>

This is all now deploy your project to your application server and give it a shot

Installing Maven on Linux, Windows and Mac

Reading Time: 3 minutes

Description

This post will show you to install and configure maven on Windows, Linux and Mac. Before performing the installation, make sure the Java Development Kit is properly installed and configured. For Linux you can refer to my post

Linux

For this installation I have performed the task on Ubuntu 18.06 LTS 64 bit, but also referred the same for other distros, it all turned out to be working perfectly. You can use the manual way to perform installation on other distros. For this make sure the java

Installing using the Package Manager

sudo apt-get install maven

Installing Manually

Navigate to Apache Maven’s official web site and and download the latest version (3.5.4 as of writing this article) the binary tar.gz package.

1. Unpack the Tar file

tar zxvf apache-maven-3.5.4-bin.tar.gz 

2. Move the folder to the opt folder

sudo mv apache-maven-3.5.4 /opt/

3.Create a Maven profile file

sudo nano /etc/profile.d/maven.sh

3.1. Add these lines in the “.sh” file

export M2_HOME=/opt/apache-maven-3.5.4
export M2=$M2_HOME/bin 
PATH=$M2:$PATH

4. Reload the “sh” file in the system

source /etc/profile.d/maven.sh
. /etc/profile.d/maven.sh

5. Check the Maven installation

mvn --version
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T20:33:14+02:00)
Maven home: /opt/apache-maven-3.5.4
Java version: 1.8.0_181, vendor: Oracle Corporation, runtime: /usr/java/jdk1.8.0_181/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-23-generic", arch: "amd64", family: "unix"

Windows

Download the binary file in .zip or tar.gz does not matter extract the folder under C drive if you want you may create the folder wherever you want, but to make it easier you shall consider practical way of it, after you extract the folder

Right Click Computer and click on Properties

windows1

On the left side of the properties, click on Advanced system settings

windows2

Below click on Environment Variables

windows3

Click on “New” on Users variables and enter the details as below

windows4

and on system variables find PATH and add the line

windows5

and now lets launch command line and see if its configured

windows6

Mac OS

Normally maven comes out of package on mac os operating systems since 10.6.8 Snow Leopard, test if the maven is installed on your mac launch terminal and enter the command:

mvn -version
Apache Maven 3.0.3 (r1075438; 2011-02-28 19:31:09+0200)
Maven home: /usr/share/maven
Java version: 1.7.0_45, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.5", arch: "x86_64", family: "mac"
Tugruls-Mac:~ tugrulaslan$

to install it manually as the above linux package file download and exract the file the same way

tar -zxvf apache-maven-3.1.1-bin.tar.gz
sudo mv apache-maven-3.1.1 /usr/lib/maven
vim ~/.bash_profile

and these lines

export M2_HOME=/usr/lib/maven
export PATH=$PATH:$M2_HOME/bin

before you proceed the maven installation, please make sure that you have successfully carried out jre and jdk installation and configuration

Maven goals

Reading Time: 1 minute
  • validate – validate the project is correct and all necessary information is available
  • compile – compile the source code of the project
  • test – test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package – take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test – process and deploy the package if necessary into an environment where integration tests can be run
  • verify – run any checks to verify the package is valid and meets quality criteria
  • install – install the package into the local repository, for use as a dependency in other projects locally
  • deploy – done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

Creating a Maven web application and integration for Eclipse and IntelliJ IDEs

Reading Time: 1 minute

Hi folk

In this tutorial I will show you how you can create a dynamic web project using maven in command line. First of all if you would like to create a web app rapidly then issue the command below, or if you would like to create and browse different type of projects remove the project type and other specifications in the below given command. On Linux, Windows or Mac does not matter launch your terminal/command line app to create our project:

mvn archetype:generate -DgroupId=PROJECTPACKINGNAMEGOESHERE -DartifactId=PROJECTNAMEGOESHERE -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

As soon as you launch this code the outcome should look like this:

Selection_016

our project is ready and lets convert our project into an Eclipse or IntelliJ project issue the command below but first of all navigate to the project folder first otherwise you will have the errors maven unable to locate the pom.xml file:

Eclipse Integration

mvn eclipse:eclipse -Dwtpversion=2.0

IntelliJ Integration

mvn idea:idea -Dwtpversion=2.0

After the command our project will look and converted to that IDE structure, the below the folder structure since I am an IntelliJ fan the here is my folder output:

Selection_017

Deploying Maven Projects to Tomcat

Reading Time: 2 minutes

In this tutorial I will show you how to easily deploy your project on your favorite. We have a few of code and file changes thats all. First of all lets start out with the pom file. We need maven tomcat plugin to deploy our war file, copy and paste the below code between build tags in your pom.xml file and edit it the way you want

<plugins>  
    <!-- this maven plugin directly does deploy the project to tomcat7 -->
    <!-- also check out tomcat users and maven settings for the equality of credentials -->
    <!-- if credentials do not match maven will fail to deploy the project to AS -->
          <plugin>
              <groupId>org.apache.tomcat.maven</groupId>
              <artifactId>tomcat7-maven-plugin</artifactId>
              <version>2.1</version>
              <configuration>
                  <url>http://localhost:8080/manager/text</url>
                  <!-- Refer to the server settings in your ~/.m2/settings.xml -->
                  <server>dev-tomcat</server>
                  <path>/PROJECTNAME</path>
              </configuration>
          </plugin>
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>3.0</version>  
                <configuration>  
                    <source>${jdk.version}</source>  
                    <target>${jdk.version}</target>  
                </configuration>  
            </plugin>  
        </plugins>

We now need to alter our tomcat-users.xml file which is located under conf folder in your main tomcat file. Add the line below in your tomcat-users.xml file

<tomcat-users>
 <role rolename="manager-gui"/>
 <role rolename="manager-script"/>
 <role rolename="manager-jmx"/>
 <role rolename="manager-status"/>
 <role rolename="admin-gui"/>
 <role rolename="admin-script"/>

 <user username="tomcat" password="password"  roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/>
</tomcat-users>

And now one more file left to be altered, settings.xml file once again located in conf folder in your main maven folder. Add this line between servers tag

<server>
   <id>dev-tomcat</id>
   <username>tomcat</username>
   <password>password</password>
  </server>

We are all setup right now. To give you the advice if you are working on STS or Eclipse you will need to add m2e plugin from eclipse market to run the desired project with the goal

mvn clean tomcat7:deploy

If you redeploy it please do not forget to change the goal. If your favorite IDE is IntelliJ then it is still easy. Expand Maven Projects side on the right(If you cant find it navigate to View > Tool Buttons) and go to Plugins expand tomcat7 and click on tomcat7:deploy

After these settings navigate to your serverpath/projectname to see all works.