Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

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

Avatar

Level 9

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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

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.

Avatar

Community Advisor

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...

Hope that helps!

Regards,

Santosh