Java class for builder and dependency management

3

I have projects using gradle as dependency and build manager, I know the power of the gradle, but not the domino, this matter of being a dynamic language (groovy) forces you to know 100% of the commands you type! It does not have code completion much less javaDoc, I think this makes it very difficult for anyone who is new to the tool, usually Java developers have initial difficulty because it is another way to "program".

On the other hand a great advantage in the gradle that I see is precisely because it is a script language, that way we are freer to program, we can program in the script itself, it is very cool, in java it would not be possible .. .

Well, here I was thinking, why could not I create a Java class even if we were able to manage the dependencies of the project as well as the build?

See, it's not a matter of reinventing the wheel, but it's always good to think of alternatives that are meant to make our work easier, that's how things work, if we were to think that way (reinvent the wheel) Maven or Gradle would exist.

Questions, what would be the difficulties?

Can not java play this role?

Is there already a tool (or java class) that already does this?

In short, the way I think, the "script" would be an instantiated class in Java with all the necessary project settings!

    
asked by anonymous 22.12.2014 / 16:30

1 answer

3

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.

    
22.12.2014 / 17:29