It is perfectly possible to do this with Java, examples are Ant and Maven.
Ant
Ant is a library for builds that is usually configured 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 quite simple, just extend the original Task :
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, it's pretty simple.
Maven
I did not do this kind of "joke" with Maven, but apparently it's pretty simple.
See the example of this SOen response :
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 a 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 Maven scope.
The greatest care you need to take, for Maven or 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 much needed for builds . The hardest part is knowing how to do it in the most direct way possible.
I have been working with Maven for some years and this was only necessary in cases where the organization of projects was too complex. However, the ideal would be to reorganize the projects. Knowing Maven better and how to organize the files well in the project I needed less and less of gambiarras in my builds .
In some cases, you can merge the 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 just as complicated to maintain as the application itself.
Another consideration is that the Java language is very verbose and not flexible, which makes it not very suitable for build automation tasks. Script 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 schedule is difficult, but after some time it may be worth it.
I do not tell you to give up trying to do something better or just reinvent the wheel. But do not lose sight of how many of the existing projects are done by very experienced people and you can learn a lot from them.