Adding artifacts to module through modifiableArtifactModel

I want to add a new artifact to the module programatically when the module is created. I have to add libraries to the module settings, can I can add new artifcat settings so that a jar is generated and copied to a custom location. I have got the ModifiableArtifactModel through ArtifactManagerImpl



     ArtifactManager artifactManager = ArtifactManagerImpl.getInstance(project);

     ModifiableArtifactModel modifiableArtifactModel = artifactManager.createModifiableModel();

     ArtifactRootElementImpl artifactRootElement = new ArtifactRootElementImpl();

     //how to add current project's compilable units as a child to artifactRootElement??

     modifiableArtifactModel.addArtifact("my artifact", new JarArtifactType(), artifactRootElement);



but I can't seem to figure out how to add the current project's compiled units to jar, set the output path.

Thanks
1 comment
Comment actions Permalink

After a lot of messing around I finally figured it out

        ArtifactManager artifactManager = ArtifactManagerImpl.getInstance(project);
        ModifiableArtifactModel modifiableArtifactModel = artifactManager.createModifiableModel();
        ArchivePackagingElement archivePackagingElement = new ArchivePackagingElement(module.getName() + ".jar");

        final PackagingElement<?> moduleOutput = PackagingElementFactory.getInstance().createModuleOutput(module);
        archivePackagingElement.addFirstChild(moduleOutput);
        final ModifiableArtifact artifact = modifiableArtifactModel.addArtifact("Project Artifacts", JarArtifactType.getInstance(), archivePackagingElement);
        artifact.setOutputPath(libsDir.getAbsolutePath());
        artifact.setBuildOnMake(true);


It would be nice if there was at least some proper javadoc

0

Please sign in to leave a comment.