Java class for Builder and dependency management

Asked

Viewed 181 times

3

I have projects using Gradle as a dependency and build manager, I know the power of Gradle, but not the domino, this issue of being a dynamic language(groovy) forces you to know 100% of the commands you type! There is no code Completion much less javadoc, I believe that this makes it very difficult for those who are beginner in the tool, usually Java developers have difficulty starting, because it is another way of "programming".

On the other hand a great advantage in Gradle that I see is precisely because it is "script" language, in this way we become more free to program, we can program in the script itself this is very cool, in java it would not be possible...

Well, here I was thinking, because I couldn’t create a Java class even if we could manage the project dependencies as well as build?

You see, it’s not a question of reinventing the wheel, but it’s always good to think of alternatives that aim to facilitate our work, that’s how things work,.

Questions, what would be the difficulties?

Java is not able to play that role?

There is already a tool (or class in java) that does this?

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

  • 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.

  • Know a project that already wraps the Maven API?

1 answer

3


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.

  • 2

    "Another consideration is that the Java language is very verbose and not very flexible, which makes it not very suitable for build automation tasks". Spoke and said. I don’t see myself trading sbt, Gradle or Buildr Dsls for using Maven / Ant raw Apis in Java. I even prefer a giant, opinionated XML like Maven’s pom.xml than trying to do it for the Apis. Of course code Completion is desirable (and Intellij even does a good job with sbt), but I’d rather edit sbt build files at hand than have to deal with Java verbosity for such a task.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.