Archive

Posts Tagged ‘maven’

How to create a simple web app project with maven and eclipse and push to git

February 13th, 2015 No comments

here are normally 2 ways to build a simple web app project with maven and eclipse.

1. Using Maven Command line tool to  simple web app project
2. Use Eclipse to build simple  maven web project

Using Maven Command line tool to  simple web app project

Before we dive into details, let us understand what is archetype which we will be talking about in this blog ?

Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made

Using Maven Command line tool to  simple web app project

To generate a simple maven web app project do this but the simple approach which I would advice is use command prompt

$ mvn archetype:generate -DgroupId=com.skg
-DartifactId=MyWebApp
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Use Eclipse to build simple  maven web project

Select

File–>New–>Project

Chose Maven Project

use this archetype

ma

Will create a simple web project in eclipse for you

 

To build a simple project (non web based) and push to it

 

mvn archetype:generate -DgroupId=com.skg.social -DartifactId=socialIntegration -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

git eclipse:eclipse

Steps
1. Create a eclipse project using command line
2. go to directory of your porject for eg helloSpring
3. git init
4. git add * (adds all files recursively in directory to git)
5. git commit -m “first message”
6. Create a repo on git through their UI or command line
7. git remote add origin https://<url>/<username>/<project-name>.git
8. git push -u origin –all # pushes up the repo and its refs for the first time

This displays the remote url which is configured
git config remote.origin.url

git push origin local-name:remote-name

 

How to create a java project with Maven in Eclipse

April 28th, 2014 No comments

There are two way to get started with java project in Maven

1. Use command line tool

2. Use Eclipse or STS.

In this port we will demonstrate how to create a maven project in Eclipse or STS (Spring source tool Suite)

 

Using Eclipse/ STS to create Java Maven Project

  • Click on File –>New Project .
  • Select the Maven project.
  • In next step, give the name and then go to next step of selecting the archetype. This is important. If you skip the archetype selection, you will no longer be able to run java code.

Arhchetype are maven project templating toolkit. Fastest and easiest way to get started. For more on Archetype refer to maven Arhchetype

maven-java-project

  • Select maven-archetype-quickstart. Eclipse will try to download if not already present.
  • Fill in the details and click finish
  • You would be able to see  the project in your Eclipse.
  • To verify , this is how the Java Build path should look like in Eclipse
  • Maven java project

Maven Surefire for Integration Tests in JUnit

June 14th, 2011 1 comment

Maven surefire plugin is used to run unit tests during test phase of build lifecycle . The reports are generated in .txt or .xml file. These files are generated at ${basedir}/target/surefire-reports

Surefire runs unit tests during build phase, not integration tests which are executed during package phase. But you can include integration tests also to be run

Below is the configuration for surefire plugin in your pom.xml

<profile>
<id>itest</id>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.report.version}</version>
<configuration>
<includes>
<include>**/*IntTest.java</include>
<include>**/*IntTests.java</include>
</includes>
</configuration>
</plugin>

</plugins>
</build>
</profile>

Categories: Programming / tutorials Tags: ,

Adding a definition in maven

December 29th, 2010 No comments

So you have a jar file which you would like to add in maven and not sure what the definition would be . Here is what you do

1. Go to http://mvnrepository.com/

2.  Search for file you want to add for eg c3p0

3. You might see couple of results matching your search.

4. Find the definition that matches your need  for eg in this case

<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency>

Categories: Programming / tutorials Tags:

Configuring c3p0 connection pool with maven

December 29th, 2010 No comments

Let us say you want to configure c3p0 connection pool in your application through maven.

Here is the definition you need to add in pom.xml

<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency>

If you would like to see  how to configure c3po connection pool in Spring see this post Using connection pool with MYSQL in Spring applicationContext.xml

Duplicate class warnings when using shade plugin

December 24th, 2010 2 comments

In the last post, I had talked about Building an executable jar with Maven

The same can also be achieved using Shade plugin. Below are the changes you will do to build an executable jar with Maven’s shade plugin.

If you are using a shade plugin

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation

=”org.apache.maven.plugins.shade.resource.ManifestResourceTransformer”>
<mainClass>com.your.executable.class.name</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

Then you might see some warnings like

[WARNING] We have a duplicate net/sf/cglib/util/StringSwitcher$StringSwitcherKey.class in C:\dev\repository\cglib\cglib-full\2.0.2\cglib-full-2.0.2.jar

What is fix for this ?

Use assembly plugin as described in here. Building executable jar with maven

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation=”org.apache.maven.plugins.shade.resource.ManifestResourceTransformer”>
<mainClass>com.fmcna.cmd.digicomm.DigicommController</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Categories: Programming / tutorials Tags:

Building an executable jar with Maven

December 23rd, 2010 6 comments

This is one of the most commonly asked questions on Maven.

How to build executable jar with maven.

The idea is to build a jar with all its dependencies so that you can execute java – <nameof jarfile.jar>

Here is how to do it in maven. Add this to your pom.xml

<build>
<plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fully.qualified.nameOfCLass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

You can run mvn assembly:assembly and a jar file with all the dependencies will be built.

This is good if this is a standalone project. But if you have a parent pom and there is sub project which needs an executable jar, use goal single to build it along with the rest of the application.

Here is the pom.xml for this
Read more…

Categories: Programming / tutorials Tags:

Integrating selenium with maven

September 25th, 2010 1 comment

In the last post , we talked about Unit testing with Selenium. In this post , we will talk about how to integrate selenium with Maven and run your unit tests with maven. It assumes that you are already familiar with Maven.

Add the following dependency and plugin into your pom.xml

<dependency>

<groupId>org.seleniumhq.selenium.client-drivers</groupId>

<artifactId>selenium-java-client-driver</artifactId>

<version>1.0.1</version>

<scope>test</scope>

</dependency>

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>selenium-maven-plugin</artifactId>

<executions>

<execution>

<phase>pre-integration-test</phase>

<goals>

<goal>start-server</goal>

</goals>

<configuration>

<background>true</background>

</configuration>

</execution>

</executions>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<configuration>

<skip>true</skip>

</configuration>

<executions>

<execution>

<id>integration-test</id>

<phase>integration-test</phase>

<goals>

<goal>test</goal>

</goals>

<configuration>

<skip>false</skip>

<excludes>

<exclude>none</exclude>

</excludes>

<includes>

<include>**/*Test.java</include>

</includes>

</configuration>

</execution>

</executions>

</plugin>

Keep all you tests into web-app/src/test/java folder.

Setup classpath :

Add selenium-java-client-driver.jar, selenium-server.jar and junit.jar in the classpath.

Run tests:

Execute the test from command line with the command – mvn install