简体   繁体   English

无法在Spring Boot项目中使用自定义jar

[英]unable to use custom jar in spring boot project

I am trying to use one of the services of spring boot app into another spring boot service. 我正在尝试将spring boot应用程序的服务之一使用到另一个spring boot服务中。 Due to some restrictions i have to use jar based approach ie, i am building first project using command maven build and using the jar created for that project. 由于某些限制,我必须使用基于jar的方法,即,我正在使用命令maven build并使用为该项目创建的jar来构建第一个项目。

I'm adding that jar into the build path of other/dependent project. 我将该jar添加到其他/相关项目的构建路径中。 But I'm not able to see the services of my main project. 但是我看不到主项目的服务。 neither i'm able to autowire them. 我也无法自动接线。 Few days ago, somehow i was able to see the services but the maven build for dependent project was failing as it was unable to find the source package of autowired service in dependent project (an obvious failure as that package was in main project). 几天前,我以某种方式能够看到服务,但是针对依赖项目的Maven构建失败了,因为它无法在依赖项目中找到自动装配服务的源包(由于该包位于主项目中,因此显然是失败的)。 i have tried many things but i'm not sure how to proceed now. 我已经尝试了很多事情,但是现在不确定如何进行。

MainProject 主项目

JartestApplication.java JartestApplication.java

@SpringBootApplication
public class JartestApplication 
{

    public static void main(String[] args) throws Exception 
    {
        SpringApplication.run(JartestApplication.class, args);
    }

}

Service.java Service.java

@FunctionalInterface
public interface DbService 
{
    public BigInteger getRowCount(String tableName) throws Exception;
}

ServiceImpl.java ServiceImpl.java

@Service
public class DbServiceImpl implements DbService
{

    @Autowired
    EntityManager em;

    @Override
    public BigInteger getRowCount(String tableName) throws Exception
    {
        return (BigInteger) em.createNativeQuery("select count(*) from "+tableName).getSingleResult();
    }

}

pom.xml pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>jartest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jartest</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

DependentProject 依赖项目

ImportjartestApplication.java ImportjartestApplication.java


@SpringBootApplication
public class ImportjartestApplication 
{

    public static void main(String[] args)
    {
        ApplicationContext context = SpringApplication.run(ImportjartestApplication.class, args);
        Test test = context.getBean(Test.class);
        System.err.println(test.check("test"));
    }

}

Test.java Test.java


@FunctionalInterface
public interface Test 
{
    public BigInteger check(String name);
}

TestImpl.java TestImpl.java

@Service
public class TestImpl implements Test 
{

    //@Autowired    ---  what i want  to do
    //DbService service;    --- service is not visible even after i have added the jar of main project into the build path of this project

    @Override
    public BigInteger check(String name) 
    {
        return null;
        //return service.getRowCount(name);  -- my actual aim
    }

}

Is there any other way so that i can share my service without sharing the code? 还有其他方法可以使我无需共享代码即可共享我的服务吗?

Due to some limitations i cannot expose my service as rest service so i am trying to deploy the main service as jar and adding it in build path of dependent project. 由于某些限制,我无法将我的服务公开为其余服务,因此我试图将主服务部署为jar并将其添加到依赖项目的构建路径中。

In your spring-boot-maven-plugin add following entries. 在您的spring-boot-maven-plugin添加以下条目。

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <!--- begins --->
  <configuration>
    <includeSystemScope>true</includeSystemScope>
  </configuration>
  <!--- ends --->
</plugin>
after lots of debugging and reading the docs i found the solution and hereby i'm posting the steps to it....

1. Used the below build configuration in source project

    <build>
        <finalName>generator</finalName>
        <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>

2. Included the dependency into target project as system scope 

        <dependency>
            <groupId>generator</groupId>
            <artifactId>generator</artifactId>
            <scope>system</scope>
            <version>1.0</version>
            <systemPath>${project.basedir}\src\lib\generator.jar
            </systemPath>
        </dependency>

3. Included the following build configuration in target project


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--- begins - -->
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
                <!--- ends - -->
            </plugin>
        </plugins>
    </build>

4. Used component scan at root level in target class

@SpringBootApplication
@ComponentScan("com.example.jar.service")
public class JartestApplication 
{
    public static void main(String[] args) throws IOException 
    {
          System.out.println("Hello world");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从spring boot项目创建jar,我们要在另一个spring boot应用中使用的jar? - how to create jar from spring boot project , this jar we want to use in another spring boot app? 无法将自定义 spring 引导库项目自动连接到我的应用程序 - unable to autowire custom spring boot library project to my application 无法使用 docker 构建具有自定义依赖项的 Spring Boot 项目 - Unable to build the spring boot project with custom dependency using docker Spring 引导项目 jar 作为另一个项目中的依赖项 - Spring Boot project jar as dependency in another project Heroku上的Spring Boot项目无法使用application.yml - Spring boot project on Heroku unable to use application.yml 无法在我的 Spring 引导项目中使用 Maven 依赖项 - Unable to use Maven dependency inside my Spring boot project 在多模块Spring Boot项目中无法使用@EnableJpaRepositories - Unable to use @EnableJpaRepositories in multi module spring boot project 无法在 Spring Boot 中使用自定义 HttpMessageNotReadableException 错误消息 - Unable to use custom HttpMessageNotReadableException error message in Spring Boot 无法将我的 Spring 启动应用程序打包为 Jar - Unable to package my Spring boot application as Jar 无法部署Spring Boot Jar应用程序 - Unable to deploy spring boot jar application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM