1

I have two maven projects, one depends on the other, but I can't import classes from that project, I only get package does not exist. I have tried the solutions suggested here but none of the suggested solutions seem to work.

Setup:

spring-2.5.5/bin/spring init -a foo -g com -n Foo foo
spring-2.5.5/bin/spring init -a bar -g com -n Bar bar
cat << EOF > foo/src/main/java/com/foo/FooComponent.java
package com.foo;
public class FooComponent {}
EOF
sed -i 's/\(package.*\)/\1\nimport com.foo.FooComponent;/g' bar/src/main/java/com/bar/BarApplication.java
sed -i 's/\(<dependencies.*\)/\1\n<dependency><groupId>com<\/groupId><artifactId>foo<\/artifactId><version>0.0.1-SNAPSHOT<\/version><\/dependency>\n/g' bar/pom.xml
mvn -f foo install -DskipTests
mvn -f bar compile

foo is successfully installed into ~/.m2/repository, while bar fails with bar/src/main/java/com/bar/BarController.java:[3,15] package com.foo does not exist

Any ideas? Or maybe there's some flaw in the setup that I'm missing... Thanks.

Erik Vesterlund
  • 481
  • 6
  • 19
  • A spring-boot project can be used as a dependency by another project... (I suppose that project is using spring-boot-maven-plugin to package to final jar file)..This sounds like a job for a multi module build to factor out the common code and built two different spring boot artifacts... – khmarbaise Oct 02 '21 at 20:38
  • @khmarbaise Multi-module build wouldn't be appropriate as the above is only intended for a few illustration purposes, but that's beyond the scope of the question. – Erik Vesterlund Oct 02 '21 at 20:44
  • What is the illustration purpose? That it does not work as you like that it should work? I've already written why it does not work.. ? – khmarbaise Oct 02 '21 at 20:50
  • @khmarbaise I don't see anywhere in your response as to why it might not work. All the code is what little is generated from the above, there's nothing else. Is the package "foo" not correctly installed, or what might be the reason mvn can't find it? – Erik Vesterlund Oct 02 '21 at 20:55
  • "A spring-boot project can NOT being used as a dependency by another project... (I suppose that project is using spring-boot-maven-plugin to package to final jar file)"... ?Seemed to be a typo on my side... – khmarbaise Oct 02 '21 at 21:00
  • @khmarbaise I don't really see an explanation in those two sentences. Are you suggesting the problem is with spring-boot-maven-plugin packaging? I'm not very familiar with maven but the only maven commands I'm using are the two above (`mvn -f foo install` and `mvn -f bar compile`). If I should be using something else, please advice. – Erik Vesterlund Oct 02 '21 at 21:08
  • 3
    A spring boot jar has a special contents (in contradiction to a usual jar file) which results in a way that it can not being used as a dependency. – khmarbaise Oct 02 '21 at 21:34
  • @khmar Those jars still have class files, so could you be more specific? – OneCricketeer Oct 03 '21 at 06:21
  • @Erik Why do you need to use `spring init` then weird sed tricks instead of Maven Archetypes? – OneCricketeer Oct 03 '21 at 06:23
  • 2
    The spring boot jar container a boot loader which will start later the content of the application (including tomcat/undertoe etc.) the full details can be read here: https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html#executable-jar To use a jar file it must follow a structure which a spring boot jar does not.. – khmarbaise Oct 03 '21 at 10:39
  • @OneCricketeer I'm not familiar with mvn archetypes so sed was easier for creating a minimal, reproducible example. – Erik Vesterlund Oct 03 '21 at 12:08
  • @khmar The format is optional, though. The bottom of that page gives alternatives that would work for importing elsewhere (specifically, the shade plugin) – OneCricketeer Oct 03 '21 at 13:18

1 Answers1

1

I went with @khmarbaise's indication that the packaging from mvn install might not be sufficient, and found a solution here, namely to add the following to the dependency's pom:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Then we can do:

mvn -f foo install spring-boot:repackage
mvn -f bar compile

and the package com.foo is found.

Erik Vesterlund
  • 481
  • 6
  • 19