Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

componentFinder.findComponents(); #61

Open
kjellmoens opened this issue Oct 29, 2021 · 8 comments
Open

componentFinder.findComponents(); #61

kjellmoens opened this issue Oct 29, 2021 · 8 comments

Comments

@kjellmoens
Copy link

Hi,

I have been working with the SourceCodeComponentFinderStrategy. Is it also possible to scan a compiled jar and display the annotated tags (@controller, ...) in a diagram.

Thank you,

Kjell

@kjellmoens
Copy link
Author

Hi, do you already had the time to look at my question ?

@simonbrowndotje
Copy link
Collaborator

Is it also possible to scan a compiled jar and display the annotated tags (@controller, ...) in a diagram.

It depends what you want to output to look like, but scanning a compiled JAR is available via the ComponentFinder and some pluggable strategies. See https://github.com/structurizr/java-extensions/blob/master/docs/component-finder.md for more.

@kjellmoens
Copy link
Author

Hi Simon,

Thank you for your response. Do you have some code example on how to do it, since I do not succeed in making it work.

Thank you

@simonbrowndotje
Copy link
Collaborator

@kjellmoens
Copy link
Author

Thank you but I don't see how you can specify the external jar as a parameter. We would like to build a project, create a jar and then generate the schema from the annotations in the jar from a gitlab step.

@simonbrowndotje
Copy link
Collaborator

The JAR file isn't specified as a parameter ... since reflection is done using the bytecode, you should include your production code on the classpath of your Structurizr program; for example -> https://github.com/structurizr/java-extensions/blob/master/structurizr-examples/build.gradle#L16

@kjellmoens
Copy link
Author

kjellmoens commented Feb 4, 2022

When I try to generate the components having the source code in the same project, it seems to work and he generates a plantuml file with all structurizr tags found.
But if I add the jar file to the classpath instead of the sourcecode, he also seems to find the structurizr tags but nothing is output in the plantuml file.

10:46:11.392 [main] INFO org.reflections.Reflections - Reflections took 234 ms to scan 137 urls, producing 5 keys and 22 values

Below is the code i use to generate the plantuml file.

    public static void main(String[] args) throws Exception {
        Workspace workspace = new Workspace("Structurizr for Java Annotations", "This is a model of my software system.");
        Model model = workspace.getModel();
        model.setImpliedRelationshipsStrategy(new CreateImpliedRelationshipsUnlessAnyRelationshipExistsStrategy());

        Person user = model.addPerson("User", "A user of my software system.");
        SoftwareSystem softwareSystem = model.addSoftwareSystem("Software System", "My software system.");

        Container webApplication = softwareSystem.addContainer("Web Application", "Provides users with information.", "Java");
        Container database = softwareSystem.addContainer("Database", "Stores information.", "Relational database schema");
        database.addTags(DATABASE_TAG);

        ComponentFinder componentFinder = new ComponentFinder(
                webApplication,
                "be.fgov.minfin",
                new StructurizrAnnotationsComponentFinderStrategy()
        );
        componentFinder.findComponents();


        ViewSet views = workspace.getViews();
        SystemContextView contextView = views.createSystemContextView(softwareSystem, "SystemContext", "An example of a System Context diagram.");
        contextView.addAllElements();

        ContainerView containerView = views.createContainerView(softwareSystem, "Containers", "The container diagram from my software system.");
        containerView.addAllElements();

        ComponentView componentView = views.createComponentView(webApplication, "Components", "The component diagram for the web application.");
        componentView.addAllElements();

        Styles styles = views.getConfiguration().getStyles();
        styles.addElementStyle(Tags.ELEMENT).color("#ffffff");
        styles.addElementStyle(Tags.SOFTWARE_SYSTEM).background("#1168bd");
        styles.addElementStyle(Tags.CONTAINER).background("#438dd5");
        styles.addElementStyle(Tags.COMPONENT).background("#85bbf0").color("#000000");
        styles.addElementStyle(Tags.PERSON).background("#08427b").shape(Shape.Person);
        styles.addElementStyle(DATABASE_TAG).shape(Shape.Cylinder);

        File plantUMLFile = new File("test"+ ".puml");


        FileWriter fileWriter = new FileWriter(plantUMLFile);

        PlantUMLWriter plantUMLWriter = new StructurizrPlantUMLWriter();
        fileWriter.write(plantUMLWriter.toString(componentView));
        fileWriter.close();

        File pngFile = new File("test" + ".png");


        SourceStringReader reader = new SourceStringReader(plantUMLWriter.toString(componentView));

        // Write the first image to "png"
        String desc = reader.generateImage(pngFile);

    }

@simonbrowndotje
Copy link
Collaborator

I would run in debug and/or look at what components are being added to webApplication by exporting the workspace to a JSON file. You might also try using the newer PlantUML exporters.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants