Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ JDBC drivers can be added/updated in `build.gradle` file.

Use gradle to build and assemble jar files with dependencies.
```bash
gradle fatJar
gradle shadowJar
```

## Run
Expand Down Expand Up @@ -41,3 +41,60 @@ a | generate POJOs for all the tables in database
t | list of database tables delimited by ; (semicolon). overrides `a` option
p | (optional) java package name of the POJOs. If not specified, default/blank package will be used.
d | (optional) target directory where POJOs (.Java files) are generated. If not specified, current directory will be used


## Gradle plugin

Compile fatJar

Put jar in `libs` folder.

Configure your gradle project:

```kotlin
import com.kumarvv.table2pojo.gradle.Table2PojoExt
import com.kumarvv.table2pojo.gradle.Table2PojoPlugin
import com.kumarvv.table2pojo.model.ConnectionPrefs
import com.kumarvv.table2pojo.model.UserPrefs

buildscript {
dependencies {
classpath(files("libs/table2pojo-1.0-all.jar"))
}
}


apply {
plugin(Table2PojoPlugin::class.java)
}

sourceSets {
main {
kotlin {
srcDirs(srcDirs.map { it.path })
}
java {
srcDirs("${layout.buildDirectory.get()}/generated/main/java")
}
}
}

val databaseUrl = project.findProperty("databaseUrl").toString()
val databaseUser = project.findProperty("databaseUser").toString()
val databasePassword = project.findProperty("databasePassword").toString()
val databaseDriver = project.findProperty("databaseDriver").toString()


configure<Table2PojoExt> {
userPrefs = UserPrefs()
userPrefs.dir = "${layout.buildDirectory.get()}/generated/main/java"
userPrefs.tables = arrayOf("FOO")
userPrefs.pkg = "com.example.pojo"
connectionPrefs = ConnectionPrefs()
connectionPrefs.url = databaseUrl
connectionPrefs.username = databaseUser
connectionPrefs.password = databasePassword
connectionPrefs.driver = databaseDriver
}

```
45 changes: 11 additions & 34 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
plugins {
id "com.gradleup.shadow" version "8.3.0"
}

apply plugin: 'java'

repositories {
mavenCentral();
}

group = 'com.kumarvv.table2pojo'
version = '1.0'

sourceCompatibility = '1.11'
targetCompatibility = '1.11'

Expand All @@ -12,41 +19,11 @@ dependencies {
implementation group: 'org.apache.commons', name: 'commons-collections4', version: '4.4'
implementation group: 'org.apache.commons', name: 'commons-text', version: '1.10.0'
implementation group: 'commons-cli', name: 'commons-cli', version: '1.5.0'

// add additional db jars
implementation group: 'com.oracle.database.jdbc', name: 'ojdbc8', version: '21.8.0.0'
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.31'
implementation group: 'org.postgresql', name: 'postgresql', version: '42.5.1'
implementation gradleApi()
}

//task fatJar(type: Jar) {
// manifest {
// attributes 'Main-Class': 'com.baeldung.fatjar.Application'
// }
// baseName = 'all-in-one-jar'
// duplicatesStrategy = DuplicatesStrategy.EXCLUDE
// from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
// with jar
//}

//jar {
// manifest {
// attributes "Main-Class": "com.kumarvv.table2pojo.Table2Pojo"
// }
//
// from {
// configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
// }
//}

task fatJar(type: Jar) {
shadowJar {
manifest {
attributes 'Implementation-Title': 'table2pojo',
'Implementation-Version': 1.0,
'Main-Class': 'com.kumarvv.table2pojo.Table2Pojo'
attributes 'Main-Class': 'com.kumarvv.table2pojo.Table2Pojo', 'Implementation-Title': 'table2pojo'
}
baseName = project.name + '-all'
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
}
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
group = 'com.kumarvv.table2pojo'
version = '1.0'
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "table2pojo"
34 changes: 34 additions & 0 deletions src/main/java/com/kumarvv/table2pojo/Table2Pojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.kumarvv.table2pojo.core.PojoWriter;
import com.kumarvv.table2pojo.core.TableReader;
import com.kumarvv.table2pojo.model.ConnectionPrefs;
import com.kumarvv.table2pojo.model.UserPrefs;
import org.apache.commons.cli.*;
import org.apache.commons.lang3.ArrayUtils;
Expand Down Expand Up @@ -53,6 +54,35 @@ public static void main(String[] args) {
new Table2Pojo().process(args);
}

public void process(UserPrefs prefs, ConnectionPrefs connectionPrefs) {
if (prefs == null) {
return;
}

if (!validate(prefs)) {
return;
}

long millis = System.currentTimeMillis();

info("connecting to database...");
Properties props = new Properties();
props.put("driver", connectionPrefs.getDriver());
props.put("url", connectionPrefs.getUrl());
props.put("username", connectionPrefs.getUsername());
props.put("password", connectionPrefs.getPassword());
try (Connection conn = connect(props)) {
millis = System.currentTimeMillis();
info("processing tables...");
start(prefs, conn);
} catch (Exception e) {
error(e.getMessage());
} finally {
long elapsed = System.currentTimeMillis() -millis;
info("ALL DONE! (elapsed: " + elapsed + "ms)");
}
}

/**
* process
* @param args
Expand Down Expand Up @@ -168,6 +198,10 @@ private boolean validate(final UserPrefs prefs) {
private Connection connect() throws Exception {
Properties props = new Properties();
props.load(new FileInputStream(getDbProperties()));
return connect(props);
}

private Connection connect(Properties props) throws Exception {

String driver = props.getProperty("driver");
String url = props.getProperty("url");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/kumarvv/table2pojo/core/PojoWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ private String toCamelCase(String str) {
* @return
*/
private String toMethodName(String str) {
return WordUtils.capitalizeFully(str, '_').replaceAll("_", "");
return WordUtils.capitalizeFully(str, '_', '.').replaceAll("_", "").replaceAll("\\.", "");
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/kumarvv/table2pojo/gradle/Table2PojoExt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.kumarvv.table2pojo.gradle;

import com.kumarvv.table2pojo.model.ConnectionPrefs;
import com.kumarvv.table2pojo.model.UserPrefs;
import org.gradle.api.Project;

public class Table2PojoExt {

public Table2PojoExt(Project project) {
}

private UserPrefs userPrefs;
private ConnectionPrefs connectionPrefs;

public ConnectionPrefs getConnectionPrefs() {
return connectionPrefs;
}

public void setConnectionPrefs(ConnectionPrefs connectionPrefs) {
this.connectionPrefs = connectionPrefs;
}

public UserPrefs getUserPrefs() {
return userPrefs;
}

public void setUserPrefs(UserPrefs userPrefs) {
this.userPrefs = userPrefs;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/kumarvv/table2pojo/gradle/Table2PojoPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.kumarvv.table2pojo.gradle;

import org.gradle.api.Plugin;
import org.gradle.api.Project;

public class Table2PojoPlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
var ext = project.getExtensions().create("table2pojo", Table2PojoExt.class, project);
project.getTasks().create("table2pojo", Table2PojoTask.class, task -> task.setExtension(ext));
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/kumarvv/table2pojo/gradle/Table2PojoTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.kumarvv.table2pojo.gradle;

import com.kumarvv.table2pojo.Table2Pojo;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;

public class Table2PojoTask extends DefaultTask {

private Table2PojoExt extension;

public void setExtension(Table2PojoExt extension) {
this.extension = extension;
}

@TaskAction
public void generateAll() {
new Table2Pojo().process(extension.getUserPrefs(), extension.getConnectionPrefs());
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/kumarvv/table2pojo/model/ConnectionPrefs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.kumarvv.table2pojo.model;

public class ConnectionPrefs {
private String driver;
private String url;
private String username;

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getDriver() {
return driver;
}

public void setDriver(String driver) {
this.driver = driver;
}

private String password;
}