Java – refactoring code
•
Java
I'm learning the gradle tool API
I have two methods, only one line of code is different I need your advice on how we can refactor these methods to avoid code duplication
The first method Will execute build All tasks in gradle
public boolean buildProject() { ProjectConnection connection = connector.connect(); BuildLauncher build = connection.newBuild(); try { build.run();// by default it executes all tasks }finally { connection.close(); } return true; }
The second method performs only the specified tasks
public boolean buildSpecificTask(String ...tasks ) { ProjectConnection connection = connector.connect(); BuildLauncher build = connection.newBuild(); build.forTasks(tasks); try { build.run(); }finally { connection.close(); } return true; }
build. For tasks (tasks) has only one line difference;
Solution
public boolean build(String ...tasks) {
public boolean build(String ...tasks) { ProjectConnection connection = connector.connect(); BuildLauncher build = connection.newBuild(); if (tasks.length > 0) { build.forTasks(tasks); } try { build.run(); }finally { connection.close(); } return true; }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码