Last active
August 29, 2015 14:21
-
-
Save msarhan/db7e919a7f15a08f5c59 to your computer and use it in GitHub Desktop.
Generate local Maven repository from existing jar files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Mouaffak A. Sarhan. | |
* | |
* This Node script will read Jar files inside 'lib' folder and: | |
* 1) Install jar files to local Maven repository 'repo' | |
* 2) Generate 'pom.xml' file | |
*/ | |
function generate(){ | |
var fs = require('fs'); | |
if(fs.existsSync('pom.xml')){ | |
fs.renameSync('pom.xml', 'pom-backup.xml'); | |
} | |
var pomText = []; | |
var verReg = /(\d+\.\d+\.\d+.*|\d+\.\d+.*)\.jar/; | |
var jars = fs.readdirSync('lib'); | |
for(var i=0; i < jars.length; i++){ | |
if(!/\.jar$/.test(jars[i])){ | |
continue; | |
} | |
var fileName = jars[i]; | |
var artifact = { | |
fileName: fileName | |
}; | |
var match = fileName.match(verReg); | |
var version = (match && match.length > 0 ? match[1] : '0.0.0'); | |
var baseName = fileName.replace(/^.*\/|\.[^.]*$/g, ''); | |
artifact.version = version; | |
artifact.name = baseName.replace(version, '').replace(/-$|_$/, ''); | |
artifact.group = artifact.name; | |
console.log('>> ' + artifact.fileName); | |
mvnInstall(artifact); | |
pomText.push(mavenDependency(artifact)); | |
} | |
var pomHeader = '<?xml version="1.0" encoding="UTF-8"?>\n\ | |
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"\n\ | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">\n\ | |
<modelVersion>4.0.0</modelVersion>\n<dependencies>'; | |
var repo = '\n<repository>\ | |
\n\t<id>project</id>\ | |
\n\t<url>file://${project.basedir}/repo</url>\ | |
\n</repository>'; | |
pomText = pomHeader + pomText.join("") + "\n</dependencies>" + repo + '\n</project>'; | |
fs.writeFileSync("pom.xml", pomText); | |
} | |
function mvnInstall(artifact){ | |
var exec = require('child_process').execSync; | |
var child; | |
var mvn = "mvn install:install-file" + | |
" -Dfile=" + "lib/" + artifact.fileName + | |
" -DgroupId=" + artifact.group + | |
" -DartifactId=" + artifact.name + | |
" -Dversion=" + artifact.version + | |
" -Dpackaging=jar" + | |
" -DlocalRepositoryPath=repo"; | |
child = exec(mvn); | |
} | |
function mavenDependency(artifact){ | |
return new String( | |
'\n\t<!-- '+artifact.fileName+' -->'+ | |
'\n\t<dependency>'+ | |
'\n\t\t<groupId>'+artifact.group+'</groupId>'+ | |
'\n\t\t<artifactId>'+artifact.name+'</artifactId>'+ | |
'\n\t\t<version>'+artifact.version+'</version>'+ | |
'\n\t</dependency>'); | |
} | |
generate(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment