java-extensions icon indicating copy to clipboard operation
java-extensions copied to clipboard

componentFinder.findComponents();

Open kjellmoens opened this issue 4 years ago • 8 comments

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 avatar Oct 29 '21 11:10 kjellmoens

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

kjellmoens avatar Nov 18 '21 12:11 kjellmoens

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.

simonbrowndotje avatar Nov 18 '21 12:11 simonbrowndotje

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

kjellmoens avatar Nov 18 '21 13:11 kjellmoens

Perhaps the Spring example?

https://github.com/structurizr/java-extensions/blob/master/structurizr-examples/src/com/structurizr/example/SpringPetClinic.java#L67

simonbrowndotje avatar Nov 18 '21 13:11 simonbrowndotje

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.

kjellmoens avatar Nov 18 '21 13:11 kjellmoens

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

simonbrowndotje avatar Nov 18 '21 13:11 simonbrowndotje

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);

    }

kjellmoens avatar Feb 04 '22 09:02 kjellmoens

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.

simonbrowndotje avatar Feb 07 '22 09:02 simonbrowndotje