how do deploy a file into the JCR via source code built via mvn clean install -PautoInstallPackage | Community
Skip to main content
Level 8
December 31, 2022
Solved

how do deploy a file into the JCR via source code built via mvn clean install -PautoInstallPackage

  • December 31, 2022
  • 2 replies
  • 932 views

This seems like a simple question, but I cant find it documented anywhere.

In our AEM project, we have several directories and content which get deployed into the AEM JCR repository and viewable in the CRX-DE editor on localhost, for example:

 

ui.apps/src/main/content/jcr_root/apps/ourapp/clientlibs/clientlib-site/css.txt

 

I notice most of the directories has a content.xml in them, and other files.

 

I need to deploy some groovy scripts to /apps/aecu-scripts/migrations

I have put these in the directory

 

ui.apps/src/main/content/jcr_root/apps/aecu-scripts/migrations

 

But they dont get deployed.

 

Im guessing I either need to generate content.xml files for each dir, or add something to the pom.xml (I cant see anything relating to the current files which do get deployed) or edit some other project file.

 

I tried manually putting the files and folders onto localhost using the CRX-DE editor, but there is no "export" option or similar which i could use to generate content.xml files.

 

Any ideas?

 

Im using eclipse with the AEM developer tools installed, but there seem to be no tools which help. There is a server section in the AEM perspective, but there seems to be no way to add an existing local cloud author instance running on 4503, there is only an option to create a new local server on 8080, which doesnt work.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Avinash_Gupta_

Hi @tb3dock 

 

Groovy files are not deploying to the server because I think you missed adding the path in content filter.xml. Go to the ui.apps META-INF\vault and add the filter root

 

<filter root=" /apps/aecu-scripts/migrations">

 

Hope this helps.

2 replies

Avinash_Gupta_
Community Advisor and Adobe Champion
Avinash_Gupta_Community Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
December 31, 2022

Hi @tb3dock 

 

Groovy files are not deploying to the server because I think you missed adding the path in content filter.xml. Go to the ui.apps META-INF\vault and add the filter root

 

<filter root=" /apps/aecu-scripts/migrations">

 

Hope this helps.

SantoshSai
Community Advisor
Community Advisor
January 1, 2023

Hi @tb3dock ,

The traditional way of having groovy script should have .content file since it's jcr:primaryType nt:file.

Next, you can create the Importer class. This will be responsible for reading and running a script. 

import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

@AllArgsConstructor
public class Importer {

    private static final String SCRIPT_HOME = "/var/groovyconsole/scripts/";

    public void script(final Binding binding, String path) throws IOException {

        // Allow the use of absolute JCR paths or local paths within /var/groovyconsole/scripts
        path = (path.startsWith("/") ? StringUtils.EMPTY : SCRIPT_HOME) + path;

        // Use the script binding's resource resolver
        final ResourceResolver resolver = (ResourceResolver) binding.getVariable("resourceResolver");

        // Fetch the script resource
        final Resource resource = resolver.getResource(path);
        if (resource == null) {
            throw new FileNotFoundException("Could not find script at " + path);
        }

        // Read the contents of the script file
        final InputStream inputStream = resource.adaptTo(InputStream.class);
        if (inputStream == null) {
            throw new IOException("Could not read contents of script at " + path);
        }

        // Combine all lines into one string
        final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        final String script = reader.lines().collect(Collectors.joining("\n"));

        // Parse and run the string as a Groovy script
        new GroovyShell(binding).parse(script).run();
    }
}

Kindly refer the below article and follow the instructions as specified.

https://levelup.gitconnected.com/aem-import-and-re-use-groovy-snippets-in-your-scripts-and-customize-the-console-cd5057ae4d6c

Hope that helps!

Regards,

Santosh

Santosh Sai