Open arundotin opened 9 months ago
My example:
<!-- generate java code from pkl -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.pkl.codegen.java.Main</mainClass>
<arguments>
<argument>-o</argument>
<argument>src/main</argument>
<argument>--generate-getters</argument>
<argument>src/main/resources/PigeonConfig.pkl</argument>
</arguments>
<includePluginDependencies>true</includePluginDependencies>
<includeProjectDependencies>false</includeProjectDependencies>
</configuration>
<dependencies>
<dependency>
<groupId>org.pkl-lang</groupId>
<artifactId>pkl-tools</artifactId>
<version>0.25.2</version>
</dependency>
</dependencies>
</plugin>
@linux-china - I tried and moved up to one level - but the app is not working. I get NPE
Below is my pom.xml
(i have added --generate-spring-boot
argument also)
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.pkl.codegen.java.Main</mainClass>
<arguments>
<argument>-o</argument>
<argument>adventureservService/src/main/</argument>
<argument>--generate-getters</argument>
<argument>--generate-spring-boot</argument>
<argument>adventureservService/src/main/resources/AppConfig.pkl</argument>
</arguments>
<includePluginDependencies>true</includePluginDependencies>
<includeProjectDependencies>false</includeProjectDependencies>
</configuration>
<dependencies>
<dependency>
<groupId>org.pkl-lang</groupId>
<artifactId>pkl-tools</artifactId>
<version>0.25.2</version>
</dependency>
</dependencies>
</plugin>
Below are the pkl
files i have
AppConfig.pkl
module com.arun.adventures.flights.AppConfig
server: Server
class Server {
endpoints: Listing<Endpoint>
}
class Endpoint {
name: String
port: UInt16
}
This is my application.pkl
amends "AppConfig.pkl"
server {
endpoints {
new {
name = "endpoint1"
port = 1234
}
new {
name = "endpoint2"
port = 5678
}
}
}
This is my generated class (if you notice Endpoint
class doesn't have spring annotation)
import java.lang.Object;
import java.lang.Override;
import java.lang.String;
import java.lang.StringBuilder;
import java.util.List;
import java.util.Objects;
import org.pkl.config.java.mapper.Named;
import org.pkl.config.java.mapper.NonNull;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
@ConstructorBinding
@ConfigurationProperties
public final class AppConfig {
private final @NonNull Server server;
public AppConfig(@Named("server") @NonNull Server server) {
this.server = server;
}
public @NonNull Server getServer() {
return server;
}
public AppConfig withServer(@NonNull Server server) {
return new AppConfig(server);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (this.getClass() != obj.getClass()) return false;
AppConfig other = (AppConfig) obj;
if (!Objects.equals(this.server, other.server)) return false;
return true;
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + Objects.hashCode(this.server);
return result;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder(100);
builder.append(AppConfig.class.getSimpleName()).append(" {");
appendProperty(builder, "server", this.server);
builder.append("\n}");
return builder.toString();
}
private static void appendProperty(StringBuilder builder, String name, Object value) {
builder.append("\n ").append(name).append(" = ");
String[] lines = Objects.toString(value).split("\n");
builder.append(lines[0]);
for (int i = 1; i < lines.length; i++) {
builder.append("\n ").append(lines[i]);
}
}
@ConstructorBinding
@ConfigurationProperties("server")
public static final class Server {
private final @NonNull List<@NonNull Endpoint> endpoints;
public Server(@Named("endpoints") @NonNull List<@NonNull Endpoint> endpoints) {
this.endpoints = endpoints;
}
public @NonNull List<@NonNull Endpoint> getEndpoints() {
return endpoints;
}
public Server withEndpoints(@NonNull List<@NonNull Endpoint> endpoints) {
return new Server(endpoints);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (this.getClass() != obj.getClass()) return false;
Server other = (Server) obj;
if (!Objects.equals(this.endpoints, other.endpoints)) return false;
return true;
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + Objects.hashCode(this.endpoints);
return result;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder(100);
builder.append(Server.class.getSimpleName()).append(" {");
appendProperty(builder, "endpoints", this.endpoints);
builder.append("\n}");
return builder.toString();
}
}
@ConstructorBinding
public static final class Endpoint {
private final @NonNull String name;
private final int port;
public Endpoint(@Named("name") @NonNull String name, @Named("port") int port) {
this.name = name;
this.port = port;
}
public @NonNull String getName() {
return name;
}
public Endpoint withName(@NonNull String name) {
return new Endpoint(name, port);
}
public int getPort() {
return port;
}
public Endpoint withPort(int port) {
return new Endpoint(name, port);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (this.getClass() != obj.getClass()) return false;
Endpoint other = (Endpoint) obj;
if (!Objects.equals(this.name, other.name)) return false;
if (!Objects.equals(this.port, other.port)) return false;
return true;
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + Objects.hashCode(this.name);
result = 31 * result + Objects.hashCode(this.port);
return result;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder(150);
builder.append(Endpoint.class.getSimpleName()).append(" {");
appendProperty(builder, "name", this.name);
appendProperty(builder, "port", this.port);
builder.append("\n}");
return builder.toString();
}
}
}
This is my FlightsService.java
@Service
public class FlightsService {
private final AppConfig.Server server;
public FlightsService(AppConfig.Server server) {
this.server = server;
}
public AppConfig.Server getServer() {
return server;
}
}
From my controller when i do System.out.println(flightsService.getServer());
This is the output i get
Server {
endpoints = null
}
I am following this instruction here https://pkl-lang.org/spring/current/usage.html to configure
pkl
for my spring boot app (using Maven build tool)But this document has only instructions for Gradle build tool. For build tools other than grade, it is been asked there to use
pkl-codegen-java
dependency which when I use it gets in to conflict withpkl-spring
dependency due to lot of duplicate classesThis is my maven dependency
When i also add
pkl-codegen-java
Then i get this error
The instructions in the pkl documentation isn't clear. Any help on this please ?
Have raised the same question in Stackoverflow as well https://stackoverflow.com/questions/78087435/how-to-use-apple-pkl-for-spring-boot-wit-maven