Adding war artifact to custom java deploy task

I trying to convert the following configuration in my build.gradle file into a custom gradle task written in java

build.gradle

configurations {
        resultArchives
      }
        artifacts{
        resultArchives file: file("path-to-war.war"), name: "name-of-deployable", type: 'war'
      }
        repositories {
        flatDir {
          name "fileRepo"
          dirs "/media/some-folder"
        }
      }
        uploadResultArchives {
        repositories {
          add project.repositories.fileRepo
        }
      }

This is how far I am with the port but maybe there is a simpler way to do this even from java:

customTask.java

@TaskAction
  void deploy() {
    Project project = getProject();
    // Create a new configuration for the result archives
    project.getConfigurations().create("resultArchives");
    ArchivePublishArtifact apa = new ArchivePublishArtifact(new AbstractArchiveTask() {
      @Override
      protected CopyActionImpl getCopyAction() {
        // TODO Auto-generated method stub
        return null;
      }
    });
          apa.setFile(new File("path-to-war.war"));
    apa.setName("name");
    apa.setType("war");
          ArtifactHandler artifacts = project.getArtifacts();
    artifacts.add("resultArchives", apa);
                // Any default impl for this?
    FlatDirectoryArtifactRepository ff = new FlatDirectoryArtifactRepository() {
      private String repoName;
      private Object[] dirs;
        @Override
      public void setName(String name) {
        repoName = name;
      }
              @Override
      public String getName() {
        return repoName;
      }
              @Override
      public void setDirs(Iterable<?> dirs) {
      }
              @Override
      public Set<File> getDirs() {
        return null;
      }
              @Override
      public void dirs(Object... dirs) {
        this.dirs = dirs;
      }
              @Override
      public void dir(Object dir) {
        // TODO Auto-generated method stub
                }
    };
          ff.setName("fileRepo");
    ff.dirs("/media/some-folder");
    project.getRepositories().add(ff);
  //
//uploadResultArchives {
//
repositories {
//
  add project.repositories.fileRepo
//
}
//}

There is a RepositoryHandler.flatDir method that creates and adds a flat dir repository, this is similar to what you are actually using in the build script (the script uses the one that accepts a configuration closure instead of a Map):

repositories {
    flatDir {
        name "fileRepo"
        dirs "/media/some-folder"
    }
}

Thanks! Inspired by your idea I now do:

project.getConfigurations().create("resultArchives");
      // Setup the artifact to upload
        Map<String, Object> artifactArgs = new HashMap<String, Object>();
    artifactArgs.put("file", new File("path-to-war.war"));
    artifactArgs.put("name", "nname");
    artifactArgs.put("type", "war");
    ArtifactHandler artifacts = project.getArtifacts();
    artifacts.add("resultArchives", artifactArgs);
      // Setup the destination
    RepositoryHandler repositories = project.getRepositories();
    Map<String, String> flatDirArgs = new HashMap<String, String>();
    flatDirArgs.put("name", "fileRepo");
    flatDirArgs.put("dirs", "/media/some-folder");
    repositories.flatDir(flatDirArgs);
      //Create the task that does the work
    Task uploadTask = project.getTasks().add("uploadResultArchives");
     ...?

But I still need the:

uploadResultArchives {
  repositories {
    add project.repositories.fileRepo
  }

As I understand ‘uploadResultArchives’ is a new task I need to create and the add the flatDir repo that I just created. But how to do this in java and execute it as the final step?

The ‘uploadResultArchives’ task is created automatically by Gradle when you add the ‘resultArchives’ configuration. It’s an instance of Upload:

Upload uploadTask = (Upload)project.getTasks().getByName("uploadResultArchives");
uploadTask.getRepositories().add(project.getRepositories().getByName("fileRepo"));