Can we add a maven dependency only after one another has been loaded?

AnonymousAlias :

I am importing a jar via maven dependency, the jar runs an infinite loop. I want to resolve some other dependencies fully first before loading the jar.

I want to load

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Wait until i receive in my logs

Exposing 2 endpoint(s) beneath base path '/actuator'
Tomcat started on port(s): 1339 (http) with context path ''

Then load my jar dependency.

What happens is it started to load them dependencies then gets caught in an infinite loop in the jar and doesn't complete.

Thanks

Mark Bramnik :

The jar doesn't run an infinite loop, the jar is a bunch of binary sources (and possibly resources, like jsons, properties files, xmls, etc.) inside the artifact.

So the work of maven as a build tool is to prepare the artifact. Once the artifact is built the maven is done, you can't influence / alter things anymore.

Now, since you have a runtime framework like spring - it can run some beans inside the jars under conditions:

  • Load the bean in a lazy manner (only when the bean's method will be called first)
  • Run methods of beans Asynchronously (by using @Async annotation)
  • Load beans only when some profile (or another condition like property) is specified.

All these are the ways to customize the behaviour of the application in the runtime with the help of spring, but again, technically maven has nothing to do here.

So in order to

avoid infinite loop inside the jar

You can find out which bean exactly causes the infinite loop (again, its not a 'jar' in general, its some specific bean)

Use @Async annotation or define your own thread pool and run the method of the bean that causes the loop there (@Async is more spring-ish way to do it)

In any case if you load the bean by spring, it has one thread to populate the application context (which is to load all the beans and use dependency injection mechanism). So if this thread gets stuck because some bean causes infinite loop during its initialization (constructor or post-construct method) - the whole application will be stuck.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=237366&siteId=1
Recommended