Groovy script to generate maven repository structure
For a project we work with clients who are not working with Maven but deliver jars in our subversion. Since we work with Maven, we need the jars in our repository. Since the amount of libraries were growing and I didn’t want to place the jars manually in the repository, I thought it was time to give groovy scripts a try. I have read ‘Groovy in action’ and it seemed simple enough to try.
Though I am sure the code can be much cleaner and nicer using Groovy syntax, I am still putting my code online. Hopefully it might come in handy for you as well.
The script does the following:
- read a folder where all the jars are checked out
- create a dest folder where the structure will be created
- create the folder structure (jarname/version/jarname-version.jar)
- write the source jars in the same structure
- create pom.xml file for the jar
Next step should be to create a parent project linking all the jars as a package to manage the dependencies and to be able to call mvn deploy. This should deploy the everything automatically to the repository.
import java.util.ArrayList;
import java.io.File;
import java.io.FileWriter;
public class OipServiceTool {
private String sourcefolder;
private String destfolder;
private List list = new ArrayList();
public static void main(String[] args) {
System.out.println("========================================");
System.out.println("============ OipServiceTool ============");
System.out.println("========================================");
OipServiceTool tool = new OipServiceTool();
// set sourcefolder and destfolder
tool.sourcefolder = "/Users/john/Documents/workspace_3.5/ws/OipServices/wsclient-libs";
tool.destfolder = "/Users/john/Documents/workspace_3.5/roo-ws/OipServiceTool/dest";
tool.create();
System.out.println("========================================");
System.out.println("=============== finished ===============");
System.out.println("========================================");
}
public void create() {
File sourceFile = new File(sourcefolder);
if (sourceFile != null && sourceFile.exists() && sourceFile.isDirectory()) {
// run through all libs in webclient-libs
sourceFile.eachFile {
if (it.name.endsWith(".jar")) {
JarFile jarFile = new JarFile(it);
System.out.println("Created jar $jarFile");
list.add jarFile;
}
};
} else {
System.out.println("Source folder is not valid.");
}
File destFile = new File(destfolder);
// - create folder structure
// - create folder with service name
// - create folder with version number
// - copy jar in folder
// - create pom.xml file
def createFolder = {
String pomXML =
"\n" +
"\n" +
"\t4.0.0\n" +
"\tcom.componence.oip.services\n" +
"\t$it.serviceName\n" +
"\t$it.versionNumber\n" +
"\t\n" +
"\t\tcom.componence.oip.services\n" +
"\t\tOipWebservicesParent\n" +
"\t\t1.0\n" +
"\t\n" +
"\t\n" +
"\t\t\n" +
"\t\t\torg.apache.cxf\n" +
"\t\t\tcxf-bundle\n" +
"\t\t\t2.2.3\n" +
"\t\t\n" +
"\t\n" +
"";
String serviceFolder = destFile.getPath() + File.separator + it.serviceName;
String versionFolder = serviceFolder + File.separator + it.versionNumber;
File serviceFile = new File(serviceFolder);
File versionFile = new File(versionFolder);
if (!serviceFile.exists())
serviceFile.mkdir();
if (!versionFile.exists())
versionFile.mkdir();
String jarName = versionFolder + File.separator + it.fileName;
String pomName = versionFolder + File.separator + it.pom;
File jar = new File(jarName);
if (jar.exists()) {
jar.delete();
}
org.apache.commons.io.FileUtils.copyFile(it.file, jar);
if (!it.sources) {
FileWriter fw = new FileWriter(pomName);
fw.write(pomXML);
fw.close();
}
};
if (destFile != null && destFile.exists() && destFile.isDirectory()) {
list.each {createFolder(it)};
}
}
class JarFile {
private String serviceName;
private String versionNumber;
private String fileName;
private File file;
private String pom;
private boolean sources;
public JarFile(File file) throws Exception {
// split the filename '-' sign and check the length
this.file = file;
this.fileName = file.getName();
this.pom = file.getName().replace(".jar", ".pom", ".zip");
String[] nameParts = fileName.split("-");
if (nameParts.length < 1) {
throw new Exception("Name '" + fileName + "' is not supported. Will be ignored.");
}
if ("sources.jar".equals(nameParts[nameParts.length - 1])) {
sources = true;
}
this.versionNumber = nameParts[1].replaceAll(".jar", "");
this.serviceName = nameParts[0];
}
public String toString() {
return "Servicename " + serviceName + "; Version " + versionNumber + "; is source: " + sources;
}
}
}