簡體   English   中英

無法在Spring Boot項目中使用自定義jar

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

我正在嘗試將spring boot應用程序的服務之一使用到另一個spring boot服務中。 由於某些限制,我必須使用基於jar的方法,即,我正在使用命令maven build並使用為該項目創建的jar來構建第一個項目。

我將該jar添加到其他/相關項目的構建路徑中。 但是我看不到主項目的服務。 我也無法自動接線。 幾天前,我以某種方式能夠看到服務,但是針對依賴項目的Maven構建失敗了,因為它無法在依賴項目中找到自動裝配服務的源包(由於該包位於主項目中,因此顯然是失敗的)。 我已經嘗試了很多事情,但是現在不確定如何進行。

主項目

JartestApplication.java

@SpringBootApplication
public class JartestApplication 
{

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

}

Service.java

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

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

<?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>

依賴項目

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


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

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
    }

}

還有其他方法可以使我無需共享代碼即可共享我的服務嗎?

由於某些限制,我無法將我的服務公開為其余服務,因此我試圖將主服務部署為jar並將其添加到依賴項目的構建路徑中。

在您的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM