Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sofa-serverless-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
<module>arklet-core</module>
<module>arklet-springboot-starter</module>
<module>sofa-serverless-adapter-ext</module>
</modules>
<module>sofa-serverless-base-loader</module>
</modules>

<dependencyManagement>
<dependencies>
Expand Down
25 changes: 25 additions & 0 deletions sofa-serverless-runtime/sofa-serverless-base-loader/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sofa-serverless-runtime</artifactId>
<groupId>com.alipay.sofa.serverless</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<version>${revision}</version>
<packaging>pom</packaging>
<artifactId>sofa-serverless-base-loader</artifactId>
<modules>
<module>sofa-serverless-spring-loader</module>
<module>sofa-serverless-spring-loader-tool</module>
</modules>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 使用说明

1. 引入打包依赖
```xml
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.15</version>
<configuration>
<outputDirectory>../../target/boot</outputDirectory>
<classifier>executable</classifier>
</configuration>
<executions>
<execution>
<id>package</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-spring-loader-tool</artifactId>
<!--最新版本0.5.6-->
<version>0.5.6</version>
</dependency>
</dependencies>
</plugin>
```
2. fat jar启动方式不变,会默认使用sofa-serverless-spring-loader的JarLauncher启动
```shell
java -jar xxx-executable.jar
```
3. 解压启动方式,Launcher需要改成com.alipay.sofa.serverless.spring.loader.JarLauncher
```shell
java -classpath xxx-executable-unpack com.alipay.sofa.serverless.spring.loader.JarLauncher
```

# 维护说明

如果改了ofa-serverless-spring-loader代码,需要先手动mvn打包,然后将sofa-serverless-spring-loader/target/sofa-serverless-spring-loader-xxx.jar复制到sofa-serverless-spring-loader-tool/src/main/resources/META-INF/loader

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-base-loader</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>sofa-serverless-spring-loader-tool</artifactId>
<version>${revision}</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-loader</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-loader-tools</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.serverless.spring.loader.tools;

import java.io.File;

import org.springframework.boot.loader.tools.Layout;
import org.springframework.boot.loader.tools.LayoutFactory;

/**
* CustomLayoutFactory
* @author zjulbj
* @daye 2023/12/26
* @version CustomLayoutFactory.java, v 0.1 2023年12月26日 14:45 syd
*/
public class CustomLayoutFactory implements LayoutFactory {
@Override
public Layout getLayout(File source) {
return Layouts.forFile(source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.serverless.spring.loader.tools;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.springframework.boot.loader.tools.CustomLoaderLayout;
import org.springframework.boot.loader.tools.Layout;
import org.springframework.boot.loader.tools.LibraryScope;
import org.springframework.boot.loader.tools.LoaderClassesWriter;

/**
* Custom Layouts
* @author zjulbj
* @daye 2023/12/26
* @version Layouts.java, v 0.1 2023年12月26日 14:45 syd
*/
public class Layouts {
private Layouts() {
}

/**
* Return a layout for the given source file.
*
* @param file the source file
* @return a {@link Layout}
*/
public static Layout forFile(File file) {
if (file == null) {
throw new IllegalArgumentException("File must not be null");
}
String lowerCaseFileName = file.getName().toLowerCase(Locale.ENGLISH);
if (lowerCaseFileName.endsWith(".jar")) {
return new Jar();
}
return org.springframework.boot.loader.tools.Layouts.forFile(file);
}

/**
* Executable JAR layout.
*/
public static class Jar extends org.springframework.boot.loader.tools.Layouts.Jar implements
CustomLoaderLayout {
@Override
public String getLauncherClassName() {
return "com.alipay.sofa.serverless.spring.loader.JarLauncher";
}

@Override
public void writeLoadedClasses(LoaderClassesWriter writer) throws IOException {
writer.writeLoaderClasses("META-INF/loader/spring-boot-loader.jar");
writer.writeLoaderClasses("META-INF/loader/sofa-serverless-spring-loader.jar");
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.boot.loader.tools.LayoutFactory=com.alipay.sofa.serverless.spring.loader.tools.CustomLayoutFactory
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.serverless.spring.loader.tools;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.JarFile;

import com.alipay.sofa.serverless.spring.loader.tools.Layouts.Jar;
import junit.framework.TestCase;
import org.junit.Assert;

import org.springframework.boot.loader.tools.JarWriter;
import org.springframework.boot.loader.tools.Layout;

public class LayoutsTest extends TestCase {

public void testForNullFile() throws IOException {
IllegalArgumentException exception = null;
try {
Layouts.forFile(null);
} catch (IllegalArgumentException e) {
exception = e;
}
assertNotNull(exception);
}

public void testForFile() throws IOException {
CustomLayoutFactory customLayoutFactory = new CustomLayoutFactory();

File appJar = new File(getClass().getClassLoader().getResource("demo-executable.jar")
.getFile());
Layout layout = customLayoutFactory.getLayout(appJar);
assertTrue(layout instanceof Jar);
Jar jar = (Jar) layout;
assertEquals("com.alipay.sofa.serverless.spring.loader.JarLauncher",
jar.getLauncherClassName());
File rewrite = new File(appJar.getParent() + "/demo-executable-rewrite.jar");
JarWriter writer = new JarWriter(rewrite);
jar.writeLoadedClasses(writer);
writer.close();
URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { rewrite.toURI().toURL() },
null);
try {
urlClassLoader.loadClass(jar.getLauncherClassName());
} catch (ClassNotFoundException e) {
Assert.fail(e.getMessage());
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-base-loader</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sofa-serverless-spring-loader</artifactId>
<version>${revision}</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-loader</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-loader-tools</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

</project>
Loading