Kettle custom jar package for use javascript

We all know that Kettle is a Java language development, and can call the java class method directly in JavaScript inside. So sometimes, we can customize a number of methods to use for JavaScript.

This article has self-reference: https://www.xiaominfo.com/2019/08/13/kettle-12/

First, create tools in java project

In the project, create utils tools, such as a method to calculate the total number of pages. code show as below:

public class PaginationUtils {

  /**
   * 计算得到总页码
   * @param totalRecords 总记录数
   * @param pageSize 分页大小
   * @return 总页码
   */
  public static int totalPage(String totalRecords,String pageSize){
    int totalPage=0;
    try{
      BigDecimal records=new BigDecimal(totalRecords);
      BigDecimal size=new BigDecimal(pageSize);
      BigDecimal _tmp=records.add(size).subtract(new BigDecimal(1));
      BigDecimal _tp=_tmp.divide(size).setScale(0,BigDecimal.ROUND_HALF_UP);
      totalPage=_tp.intValue();
    }catch (Exception e){
      //error
    }
    return totalPage;
  }
}

Second, the deployment package

After completion of the development tools based methods, can mvn clean package -DskipTestsbe packaged command in the target directory, it will generate a jar file. This needs to be put under the kettle jar package lib directory. As shown below:

Third, script writing JavaScript

Restart the Kettle, the new JavaScript script, the total number of pages js code calculated as follows:

//计算总页码
var totalPage=com.study.spring.Utils.PaginationUtils.totalPage(countBySql,pageSize);

In fact, declare java class method + js code.

But here, kettle when running this script JavaScript, suggesting that such errors the following:

不能编译 javascript: org.mozilla.javascript.EcmaError: TypeError: Cannot call property totalPage in object [JavaPackage com.study.spring.Utils.PaginationUtils]. It is not a function, it is "object". (<cmd>#22)

The error message point of view, but it is still no way to find related classes, and did not put the jar package as ...

Four, FAQ

The above error is exactly how it happened? I find it very strange. So I use compression tool also looked at the other jar package structure lib directory, find people like this formula:

And I just hit the jar package directory is like this:

com directory and not in the root directory of the jar package, of course, is not the access method.

Well, to find the causes of the problem, and then tell you the solution:

In the pom.xml file, add:

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

After the effect of adding FIG pom file as follows:

jar pack internal structure again labeled as follows:

success! com root directory has a lower jar package.

The jar instead of under the kettle to the lib directory, restart Kettle, once again successfully invoke custom jar package through javascripts!


Point of attention, do not get lost

好了各位,以上就是这篇文章的全部内容了,能看到这里的人呀,都是人才

白嫖不好,创作不易。各位的支持和认可,就是我创作的最大动力,我们下篇文章见!

如果本篇博客有任何错误,请批评指教,不胜感激 !

Guess you like

Origin www.cnblogs.com/createboke/p/12232680.html