Installing a File into Local Repository

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Installing a File into Local Repository

Andreas Schaefer-2
Hi

I want to install a file into my local repository. The code below did work at one time but now it is creating the file in the local repository but that file is empty. Any idea on how to fix it or is there another way to do that?

Current Maven version:

mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Java/maven3
Java version: 1.8.0_201, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.6", arch: "x86_64", family: “mac"

Requirements:

This code is installing a generated file (JSon file) into the local repository with the extension ’slingosgifeature’. The packaging is ‘jar’ as this project creates a JAR file. That said it needs also to install that file into the local repository.

Code:

private void installFMDescriptor(File file, Feature feature) {
    Collection<org.apache.maven.artifact.Artifact> artifacts = Collections.synchronizedCollection(new ArrayList<>());
    if(file.exists() && file.canRead()) {
        getLog().debug("FM File to be installed: " + file.getAbsolutePath());
        // Need to create a new Artifact Handler for the different extension and an Artifact to not
        // change the module artifact
        DefaultArtifactHandler fmArtifactHandler = new DefaultArtifactHandler("slingosgifeature");
        ArtifactId artifactId = feature.getId();
        DefaultArtifact fmArtifact = new DefaultArtifact(
            artifactId.getGroupId(), artifactId.getArtifactId(), artifactId.getVersion(),
            null, "slingosgifeature", null, fmArtifactHandler
        );
        fmArtifact.setFile(file);
        artifacts.add(fmArtifact);
        try {
            installArtifact(mavenSession.getProjectBuildingRequest(), artifacts);
        } catch (MojoExecutionException e) {
            getLog().error("Failed to install FM Descriptor", e);
        }
    } else {
        getLog().error("Could not find FM Descriptor File: " + file);
    }
}

private void installArtifact(ProjectBuildingRequest pbr, Collection<org.apache.maven.artifact.Artifact> artifacts )
    throws MojoExecutionException {
    try {
        installer.install(pbr, artifacts);
    } catch ( ArtifactInstallerException e ) {
        throw new MojoExecutionException( "ArtifactInstallerException", e );
    }
}

Cheers - Andy Schaefer
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Installing a File into Local Repository

Anthony Whitford
Have you considered using https://www.mojohaus.org/build-helper-maven-plugin/attach-artifact-mojo.html <https://www.mojohaus.org/build-helper-maven-plugin/attach-artifact-mojo.html>
Or http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html <http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html> ?

Perhaps inspecting the source code for these plugins will help you.


> On Jan 24, 2020, at 8:18 AM, Andreas Schaefer <[hidden email]> wrote:
>
> Hi
>
> I want to install a file into my local repository. The code below did work at one time but now it is creating the file in the local repository but that file is empty. Any idea on how to fix it or is there another way to do that?
>
> Current Maven version:
>
> mvn --version
> Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
> Maven home: /Java/maven3
> Java version: 1.8.0_201, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre
> Default locale: en_US, platform encoding: UTF-8
> OS name: "mac os x", version: "10.14.6", arch: "x86_64", family: “mac"
>
> Requirements:
>
> This code is installing a generated file (JSon file) into the local repository with the extension ’slingosgifeature’. The packaging is ‘jar’ as this project creates a JAR file. That said it needs also to install that file into the local repository.
>
> Code:
>
> private void installFMDescriptor(File file, Feature feature) {
>    Collection<org.apache.maven.artifact.Artifact> artifacts = Collections.synchronizedCollection(new ArrayList<>());
>    if(file.exists() && file.canRead()) {
>        getLog().debug("FM File to be installed: " + file.getAbsolutePath());
>        // Need to create a new Artifact Handler for the different extension and an Artifact to not
>        // change the module artifact
>        DefaultArtifactHandler fmArtifactHandler = new DefaultArtifactHandler("slingosgifeature");
>        ArtifactId artifactId = feature.getId();
>        DefaultArtifact fmArtifact = new DefaultArtifact(
>            artifactId.getGroupId(), artifactId.getArtifactId(), artifactId.getVersion(),
>            null, "slingosgifeature", null, fmArtifactHandler
>        );
>        fmArtifact.setFile(file);
>        artifacts.add(fmArtifact);
>        try {
>            installArtifact(mavenSession.getProjectBuildingRequest(), artifacts);
>        } catch (MojoExecutionException e) {
>            getLog().error("Failed to install FM Descriptor", e);
>        }
>    } else {
>        getLog().error("Could not find FM Descriptor File: " + file);
>    }
> }
>
> private void installArtifact(ProjectBuildingRequest pbr, Collection<org.apache.maven.artifact.Artifact> artifacts )
>    throws MojoExecutionException {
>    try {
>        installer.install(pbr, artifacts);
>    } catch ( ArtifactInstallerException e ) {
>        throw new MojoExecutionException( "ArtifactInstallerException", e );
>    }
> }
>
> Cheers - Andy Schaefer
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>