It is perfectly possible to do this with Java, examples are Ant and Maven.
Ant
Ant is a library for builds configured generally from Xmls, but nothing prevents us from running Tasks directly via Java code.
The documentation itself provides an example: Using Apache Ant™ Tasks Outside of Ant
The example is very simple, just extend to Task original:
static public void unzip(String zipFilepath, String destinationDir) {
final class Expander extends Expand {
public Expander() {
project = new Project();
project.init();
taskType = "unzip";
taskName = "unzip";
target = new Target();
}
}
Expander expander = new Expander();
expander.setSrc(new File(zipfile));
expander.setDest(new File(destdir));
expander.execute();
}
I’ve played a lot with Ant, creating and extending Tasks. Once you get the hang of it is quite simple.
Maven
I didn’t get to do this kind of "joke" with Maven, but apparently it’s pretty simple.
Take the example of Soen’s reply:
InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile( new File( "/path/to/pom.xml" ) );
request.setGoals( Collections.singletonList( "install" ) );
Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File("/usr"));
try
{
invoker.execute( request );
}
catch (MavenInvocationException e)
{
e.printStackTrace();
}
There is also project that can help a lot, called Mojo Executor. The idea is to allow a plugin to run other plugins, but nothing prevents you from using it outside the scope of Maven.
The greatest care you need to take, for Maven or for Ant, is that plugins can access context information (project) and you need to make it available via code.
Considerations
Most of the time a programming language is not so much needed for the builds. The hardest part is knowing how to do this as directly as possible.
I worked with Maven a few years ago and this was only necessary in cases where the organization of projects was too complex. However, the ideal would be to reorganize the projects. Getting to know Maven better and how to organize the files in the project I needed less and less gambiarras in mine builds.
In some cases, you can merge declarative and programmatic approaches and create plugins for Ant or Maven that represent the specific tasks we need.
The problem with using a very powerful language for tasks like this is that your build can become as or more complicated to maintain than the application itself.
Another consideration is that the Java language is very verbose and not very flexible, which makes it not very suitable for automation tasks build. Scripting languages are even preferable for this type of task.
Many people do this, but not always the best solution is to rewrite the tool in the language you are most proficient, or the way you prefer. Learning other ways to work and program is difficult, but after a while it may be worth it.
I’m not saying give up trying to do something better or just reinvent the wheel. But keep in mind that many of the existing projects are done by quite experienced people and you can learn a lot from them.
Of course it is possible to do this with Java. Ant and Maven are Java, if you want to do anything programmatically, just study the API.
– utluiz
Know a project that already wraps the Maven API?
– Rodrigo Rodrigues