Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

Sunday, March 20, 2016

Connecting to MySql from java with Maven

In this example we will be creating a simple maven java project to connect to the local mysql instance, without actually installing any jdbc connector and setting it to classpath (maven will take care of this :))

1. Add MySQL connector maven dependency in the pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<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>com.automatethebox</groupId>
    <artifactId>mysql-connect-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    </dependencies>
</project>

2. Sample java source file: Connecting to local mysql instance and listing tables;

package com.automatethebox.mysql_connect_maven;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MySqlConnectionWithMaven {

    public static void main(String[] args) {

        // Databases url (here 'information_schema' is database name)
        String databaseUrl = "jdbc:mysql://localhost/information_schema";
        String jdbcConnectorClass = "com.mysql.jdbc.Driver";
        String mysqlUser = "root";
        String myUserPass = "Admin@123";
        String query = "Select distinct(table_name) from INFORMATION_SCHEMA.TABLES";

        try {
            Class.forName(jdbcConnectorClass);
            Connection connection = DriverManager.getConnection(databaseUrl, mysqlUser, myUserPass);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);
            while (resultSet.next()) {
                String tableName = resultSet.getString(1);
                System.out.println("Table : " + tableName);
            }
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Saturday, December 12, 2015

Some helpful commands to build multi-module maven project tests

Build and execute tests from all the sub modules


  • mvn clean install

Build and execute tests from a specific sub module


  • mvn clean install -pl <module-name> -am

Build and execute a specific test from a specific sub module


  • mvn -DfailIfNoTests=false -Dtest=<test-class-name> clean install -pl <module-name> -am
  • mvn -DfailIfNoTests=false -Dtest=<test-class-name#test-method-name> clean install -pl <module-name> -am

Sunday, January 5, 2014

Tuesday, January 22, 2013

How to create Maven in-project repository


Pre-requisites :You should have Maven installed on your system.

In my example i am using "sshxcute-1.0" jar file. You can download it from link https://code.google.com/p/sshxcute/"

Follow the below steps :

1. Create a "lib" folder in your java project.(Actually the name does'nt matter you can use "library" instead or whatever you like)

2. Copy the "sshxcute-1.0" jar file in "lib" folder.

3. Open command prompt and navigate to "lib" folder, then execute following command.(In my case 'sshxcute-1.0.jar' file is at "c:\project\src\com\lib" location,so you need to change file locations in below  command accordingly)

mvn deploy:deploy-file -Durl=file:///c:\project\com\src\lib -Dfile=sshxcute-1.0.jar -DgroupId=com.automatethebox -DartifactId=automatethebox -Dpackaging=jar -Dversion=1.0

 Note that you get BUILD SUCCESS on console output after running the command.



4. Now go to your project "lib" directory, and notice that an package "com.automatethebox.apis" is created.This is your in-project maven repositoy.

5. Now Open the project "pom.xml" file

6. Add following dependency in pom.xml file :

<dependencies>
        <dependency>
            <groupId>com.automatethebox</groupId>
            <artifactId>apis</artifactId>
            <version>1.0</version>
        </dependency>
</dependencies>

7. Add following repository in pom.xml file :

<repositories>
        <repository>
            <id>project.local</id>
            <name>project</name>
            <url>file:${project.basedir}/src/com/lib</url>  
        </repository>
</repositories>

NOTE : You need to update this path in url according to your project.

8 .Then refresh the Maven dependencies for the project using your IDE Maven > Reimport option.

All done, now you can use the classes from the Jar file and maintain your Maven project with your in-project Maven repository.

AWS Certified Solutions Architect Associate - AWS Introduction - Questions

All the Best !!! Show Result !! Try Again !! ×