Wednesday, August 13, 2014

How to upgrade ANT in Eclipse ?

To upgrade the ANT into the Eclipse, First you need to download the latest version of ANT anywhere on you machine.
Once you are done with the download then go to
EclipseWindowsPreferencesAntRuntimeAnt Home and Select the downloaded folder.

Now your Eclipse will use the latest version of ANT :)

Wednesday, July 16, 2014

How to exclude properties file from the Jar file

Problem Statement : Suppose your are working on Java Project which uses Maven structure where your source code is under “/src/main/java” and all your configuration files like .properties file or xml file are under “/src/main/resources” and as a final build your final package is a jar but you want without few files from your classpath. Be default Maven plugin adds all the file which comes under resources in jar file.

Solution : You can achieve the above task by using maven-jar-plugin as follows :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <excludes>
            <exclude>**/otp.properties</exclude>
        </excludes>
    </configuration>

</plugin>

Tuesday, July 1, 2014

maven-dependency-plugin (goals “copy-dependencies”, “unpack”) is not supported by m2e

Recently While importing one Maven project into Eclipse(Juno, m2e plug-ins is already installed in Eclipse). I was getting one error in project : maven-dependency-plugin (goals “copy-dependencies”, “unpack”) is not supported by m2e


Reason: Eclipse m2e does not support execution, by copying the below code in the build tag resolve the issue.

Example:

<build>
<COPY_CODE_HERE>
</build>

Code :

<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>

<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-antrun-plugin
</artifactId>
<versionRange>
1.7
</versionRange>
<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>

Save the project and it will resolve the issue, if not then Right click on your project and update Maven(Maven → Update Project... ).


Monday, May 19, 2014

How to upgrade or downgrade data schema ?

I ll continue with this tutorial to show how to upgrade or downgrade the database schema.

Suppose we need to add one more field ManagerAddress in our existing Managers tables, To do this we need to use one feature called Migrations . It allows us to have an ordered set of steps that describe how to upgrade (and downgrade) our database schema.

Each of these steps, known as a migration, contains some code that describes the changes to be applied.
  • Tools → NuGet Package Manager → Package Manager Console
  • Run the Enable-Migrations command in Package Manager Console
The two new file get created under Migrations folder :
  • Configuration.cs : It contains the setting that Migrations will use for migrating ManagingContext.
  • <timestamp>_InitialCreate.cs – This is your first migration, it represents the changes that have already been applied to the database to take it from being an empty database to one that includes the Manager and Employee tables.
Now we will add one more field ManagerAddress to the Manager Model.

public class Manager
{
public int ManagerId { get; set; }
public String ManagerName { get; set; }
public String ManagerAddress { get; set; }
public virtual List<Employee> Employees { get; set; }
}
  • Run the Add-Migration AddManagerAddress in the Package Manager Console.

The Add-Migration command checks for changes since your last migration and scaffolds a new migration with any changes that are found. We need to  give migrations a name; in this case we are calling the migration ‘AddManagerAddress’.

The scaffolded code is saying that we need to add a ManagerAddress column, that can hold string data, to the dbo.Managers table. If needed, we could edit the scaffolded code but that’s not required in this case.

namespace CreateNewDatabase.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class AddManagerAddress : DbMigration
{
public override void Up()
{
AddColumn("dbo.Managers", "ManagerAddress", c => c.String());
}
public override void Down()
{
DropColumn("dbo.Managers", "ManagerAddress");
}
}
}

  • Run the Update-Database command in Package Manager Console. This command will apply any pending migrations to the database. Our InitialCreate migration has already been applied so migrations will just apply our new AddManagerAddress migration.
The new ManagerColumn column is now added to the Managers table in the database:



Entity Framework : How to generate model using Entity Framework Design

This is a step-by-step walkthrough which will generate the Model using Entity Framework Designer and then generate a database schema from the model. The model is stored in a an EDMX file

Pre-Requisities

We need to have Visual Studio 2013 installed to complete this walkthrough.(You can use older version too like 2010 or 2012 to complete this tutorial.)
Apart from that, You will also need to have NuGet installed on your Visual Studio.

1. Create new Application

Steps :
  • Create new Application
  • Open Visual Studio, Click File New Project
  • Click Console Application under templates Visual C# Windows
  • Enter Name as ModelFirstApplication and Select OK

2. Create Model
To create model, I will use Entity Framework Designer
  • In Solution Explorer, Right Click on your Project and then Add New Item...
  • Select Data from the left menu and then ADO.NET Entity Data Model
  • Enter Name as Managing Context and click OK
  • Now Select Empty Model from Entity Data Model Wizard and then click Finish.

You will see Entity Framework Designer is opened with a blank model, Now you can start adding entities, properties to the model.
  • Right-click on the design surface and select Add New -> Entity…
  • Enter Manager as the entity name and ManagerId as the key name and click OK
  • Right-click on the new entity on the design surface and select Add New -> Scalar Property, enter ManagerName as the name of the property.
  • Repeat the above Step and a new entity Employee with a EmployeeId as a key property.
  • Add EmployeeName ,Designation as a scalar properties to the Employee Entity.
Now we need to add one to many relationship (Association) between them.
  • Right-click on the design surface and select Add New -> Association…
  • Make one end of the relationship point to Manager with a multiplicity of One and the other end point to Employee with a multiplicity of Many.
  • Check Add foreign key properties to 'Employee' Entity and click OK
Now we can generate a database from the model and can use to read and write data.
Now we can generate a database from the model and can use to read and write data.

3. Generating the database
To generate the database
  • Right-click on the design surface and select Generate Database from Model…
  • Click New Connection… and SQL Express (.\SQLEXPRESS)and enter ModelManager as the database name.
  • Select OK and It will be asked if you want to create a new database, select Yes
  • From choose your version, Select Entity Framework 6.0 and click Next
  • Select Next and the Entity Framework Designer will calculate a script to create the database schema
  • Once the script is displayed, click Finish and the script will be added to your project and opened(In my case it is created as ManagingConext.edmx.sql)
  • Right-click on the script and select Execute, you will be prompted to specify the database to connect to, specify .\SQLEXPRESS.

4. Reading and Writing Data
If you checked the Solution Explorer, The Model are already automatically generated based on EDMX file.
Now its the time to implement the Main method in Program.cs. It will create a new instance of our context and then uses it to insert a new Manager. I have used LINQ query to retrieve all Manager entry from the database.

class Program
{
static void Main(string[] args)
{
using (var db = new ManagingContextContainer())
{
// Create and save a new Blog
Console.Write("Enter a name for a new Manager: ");
var name = Console.ReadLine();

var manager = new Manager { ManagerName = name };
db.Managers.Add(manager);
db.SaveChanges();

// Display all Blogs from the database
var query = from b in db.Managers
orderby b.ManagerName
select b;

Console.WriteLine("All Managers in the database:");
foreach (var item in query)
{
Console.WriteLine(item.ManagerName);
}

Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}

Now run the Application  :


Entity Framework : How to create code using Existing Database

This is a step-by-step walkthrough which will use existing database to create the model. We will create one console Application which will generate the model using existing databases.

Pre-Requisities

We need to have Visual Studio 2013 installed to complete this walkthrough.(You can use older version too like 2010 or 2012 to complete this tutorial.)
Apart from that, You will also need to have NuGet installed on your Visual Studio and the 6.1 or later of the Entity Framework Tools.

1. Connect to an Existing Database

Check here to see how can you connect to existing database. I have already created one database in my previous blog. I ll use the same database(CreateNewDatabase.ManagingContext) to generate the model.

2. Create the Application

  • Open Visual Studio, Click File New Project
  • Click Console Application under templates Visual C# Windows
  • Enter Name as CreateModelUsingExistingDatabase and Select OK

3. Reverse Engineering Model

Here I am going to make use of the Entity Framework Tools for Visual Studio to help us generate some initial code to map to the database.
  • In Solution Explorer, Right Click on your Project and then Add New Item...
  • Select Data from the left menu and then ADO.NET Entity Data Model
  • Enter Name as Managing Context and click OK
  • Now Select Code First From Database from Entity Data Model Wizard and then click Next.


  • Select the connection to the database you created and set Entities name too and click Next.
  • From choose your version, Select Entity Framework 6.0 and click Next.
  • Click the check box next to Tables to import all tables and click Finish


Once you are completed, you will see a number of items that have been added to the project.

App.Config : This file contains the connection string to the existing database.

<connectionStrings>
<add name="ManagingContext" connectionString="metadata=res://*/ManagingContext.csdl|res://*/ManagingContext.ssdl|res://*/ManagingContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MYNA\SQLEXPRESS;initial catalog=CreateNewDatabase.ManagingContext;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

ManagingContext : A ManagingContext class has been added to your project. This represents a session with the database, allowing us to query and save data. In the default constructor there is name=ManagingContext. This tells Application that the connection string to use for this context should be loaded from the configuration file.

public partial class ManagingContext : DbContext
{
public ManagingContext()
: base("name=ManagingContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Employee> Employees { get; set; }
public virtual DbSet<Manager> Managers { get; set; }
}

Model classes :
You will also observer Manger and Employee class are also added to the project. These are the domain classes of the Application


4. Reading & Writing Data

Now its the time to implement the Main method in Program.cs. It will create a new instance of our context and then uses it to insert a new Manager. I have used LINQ query to retrieve all Manager entry from the database.

class Program
{
static void Main(string[] args)
{
using (var db = new ManagingContext())
{
// Create and save a new Blog
Console.Write("Enter a name for a new Manager: ");
var name = Console.ReadLine();

var manager = new Manager { ManagerName = name };
db.Managers.Add(manager);
db.SaveChanges();

// Display all Blogs from the database
var query = from b in db.Managers
orderby b.ManagerName
select b;

Console.WriteLine("All Managers in the database:");
foreach (var item in query)
{
Console.WriteLine(item.ManagerName);
}

Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}


Now Run the Application :


How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...