starter icon indicating copy to clipboard operation
starter copied to clipboard

Groovy console example searching apps folder.

Open wildone opened this issue 2 years ago • 4 comments

Do you have any examples of groovy scripts searching apps folder content? Apps folder appears in resource browser but can’t be searched.

wildone avatar Dec 11 '22 14:12 wildone

Hi, I assume the problem is that you are using JCR queries. Am I right? If so, it won't work because we use Bundle Resources which can't be found by JCR queries. They require tree-traversal methods to find resources.

Here is just an example of a script listing all the components (with the usage of Sling Query):

import static org.apache.sling.query.SlingQuery.$

import org.apache.sling.query.api.SearchStrategy

def root = resourceResolver.getResource("/apps")

def components = $(root).searchStrategy(SearchStrategy.DFS).find("ws:Component")
components.iterator().forEachRemaining {component ->
    println "Found: $component" 
}

paweljozwicki avatar Dec 12 '22 12:12 paweljozwicki

Thanks I will try. Yes was using JCR queries. :)

wildone avatar Jan 02 '23 11:01 wildone

@wildone we are using Sling Feature Model. To add a new library like Sling Query, you need to declare it in your distribution/src/main/features/<project-name>.json file

The declaration is:

    {
      "id": "org.apache.sling:org.apache.sling.query:4.0.4",
      "start-order": "25"
    }

In my project it would be:

Screenshot 2023-01-02 at 14 43 37

michalcukierman avatar Jan 02 '23 13:01 michalcukierman

@wildone we are using Sling Feature Model. To add a new library like Sling Query, you need to declare it in your distribution/src/main/features/<project-name>.json file

The declaration is:

    {
      "id": "org.apache.sling:org.apache.sling.query:4.0.4",
      "start-order": "25"
    }

In my project it would be:

Screenshot 2023-01-02 at 14 43 37

Yes I am, using the archetype.

wildone avatar Jan 05 '23 21:01 wildone