Skip to content

Commit

Permalink
Generate Maven Archetype
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Morgunov committed Aug 18, 2019
1 parent ea61d67 commit 3de958f
Show file tree
Hide file tree
Showing 42 changed files with 653 additions and 295 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ install:
script:
# Test with the defaults
- sbt new file://$(pwd) --name=hello && pushd hello && sbt test && popd
# Test with custom name/organization to ensure templating is done correctly
- sbt new file://$(pwd) --name=smello --organization=org.eggsample && cd smello && sbt test
# Test with custom name/organization to ensure templating is done correctly
- sbt new file://$(pwd) --name=smello --organization=org.eggsample && pushd smello && sbt test && popd
# Test Maven Archetype
- test-archetype.sh

cache:
directories:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<archetype-descriptor xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd"
name="lagom-maven-java">

<requiredProperties>
<requiredProperty key="serviceName">
<defaultValue>hello</defaultValue>
</requiredProperty>
<requiredProperty key="serviceClassName">
<defaultValue>Hello</defaultValue>
</requiredProperty>
<!-- These properties must be valid Velocity identifiers, so cannot contain dots. -->
<requiredProperty key="scala-binary-version">
<defaultValue>2.12</defaultValue>
<validationRegex>\d+\.\d+</validationRegex>
</requiredProperty>
</requiredProperties>

<!-- If you read up on the documentation and various blog posts and question/answers out on the internet, you'll
see that the archetype descriptor actually has a nifty feature where it can be aware of different modules, and
name packages for you, and all sorts of wonderful things. The problem is, we want the names of the modules to be
dynamically selected using the above properties (eg, service1Name), but the Maven archetype plugin treats module
directories different to all other directories, where other directories can substitute any property they want
using __myPropertyName__ syntax, the module directory is hard coded to only substitute __rootArtifactId__. This
issue has been raised here: https://issues.apache.org/jira/browse/ARCHETYPE-455, a patch has existed for 3 years,
but no response or review from the maintainers. So, consequently, we can't use modules in the archetype
descriptor. -->

<fileSets>
<fileSet filtered="true" encoding="UTF-8">
<directory></directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>pom.xml</exclude>
<exclude>LICENSE</exclude>
</excludes>
</fileSet>
</fileSets>

</archetype-descriptor>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package $package$.api;
package $if(mavenArchetype.truthy)$\${package}.\${serviceName}$else$$package$$endif$.api;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonSubTypes;
Expand All @@ -8,13 +8,13 @@

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = $name;format="Camel"$Event.GreetingMessageChanged.class, name = "greeting-message-changed")
@JsonSubTypes.Type(value = $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Event.GreetingMessageChanged.class, name = "greeting-message-changed")
})
public interface $name;format="Camel"$Event {
public interface $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Event {
String getName();

@Value
final class GreetingMessageChanged implements $name;format="Camel"$Event {
final class GreetingMessageChanged implements $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Event {
public final String name;
public final String message;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package $package$.api;
package $if(mavenArchetype.truthy)$\${package}.\${serviceName}$else$$package$$endif$.api;

import akka.Done;
import akka.NotUsed;
Expand All @@ -11,12 +11,12 @@
import static com.lightbend.lagom.javadsl.api.Service.*;

/**
* The $name;format="camel"$ service interface.
* The $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$ service interface.
* <p>
* This describes everything that Lagom needs to know about how to serve and
* consume the $name;format="Camel"$Service.
* consume the $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Service.
*/
public interface $name;format="Camel"$Service extends Service {
public interface $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Service extends Service {
/**
* Example:
* curl http://localhost:9000/api/hello/Alice
Expand All @@ -32,11 +32,11 @@ public interface $name;format="Camel"$Service extends Service {
/**
* This gets published to Kafka.
*/
Topic<$name;format="Camel"$Event> helloEvents();
Topic<$if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Event> helloEvents();

@Override
default Descriptor descriptor() {
return named("$name;format="camel"$")
return named("$if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$")
.withCalls(
pathCall("/api/hello/:id", this::hello),
pathCall("/api/hello/:id", this::useGreeting)
Expand All @@ -48,7 +48,7 @@ default Descriptor descriptor() {
// go to the same partition (and hence are delivered in order with respect
// to that user), we configure a partition key strategy that extracts the
// name as the partition key.
.withProperty(KafkaProperties.partitionKeyStrategy(), $name;format="Camel"$Event::getName)
.withProperty(KafkaProperties.partitionKeyStrategy(), $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Event::getName)
)
.withAutoAcl(true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package $package$.api;
package $if(mavenArchetype.truthy)$\${package}.\${serviceName}$else$$package$$endif$.api;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package $package$.impl;
package $if(mavenArchetype.truthy)$\${package}.\${serviceName}$else$$package$$endif$.impl;

import akka.Done;
import com.fasterxml.jackson.annotation.JsonCreator;
Expand All @@ -10,13 +10,13 @@
import lombok.Value;

/**
* This interface defines all the commands that the $name;format="Camel"$Entity supports.
* This interface defines all the commands that the $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Entity supports.
* <p>
* By convention, the commands should be inner classes of the interface, which
* makes it simple to get a complete picture of what commands an entity
* supports.
*/
public interface $name;format="Camel"$Command extends Jsonable {
public interface $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Command extends Jsonable {
/**
* A command to switch the greeting message.
* <p>
Expand All @@ -26,7 +26,7 @@ public interface $name;format="Camel"$Command extends Jsonable {
@SuppressWarnings("serial")
@Value
@JsonDeserialize
final class UseGreetingMessage implements $name;format="Camel"$Command, CompressedJsonable, PersistentEntity.ReplyType<Done> {
final class UseGreetingMessage implements $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Command, CompressedJsonable, PersistentEntity.ReplyType<Done> {
public final String message;

@JsonCreator
Expand All @@ -44,7 +44,7 @@ final class UseGreetingMessage implements $name;format="Camel"$Command, Compress
@SuppressWarnings("serial")
@Value
@JsonDeserialize
final class Hello implements $name;format="Camel"$Command, PersistentEntity.ReplyType<String> {
final class Hello implements $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Command, PersistentEntity.ReplyType<String> {
public final String name;

@JsonCreator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package $package$.impl;
package $if(mavenArchetype.truthy)$\${package}.\${serviceName}$else$$package$$endif$.impl;

import akka.Done;
import com.lightbend.lagom.javadsl.persistence.PersistentEntity;

import java.time.LocalDateTime;
import java.util.Optional;

import $package$.impl.$name;format="Camel"$Command.Hello;
import $package$.impl.$name;format="Camel"$Command.UseGreetingMessage;
import $package$.impl.$name;format="Camel"$Event.GreetingMessageChanged;
import $if(mavenArchetype.truthy)$\${package}.\${serviceName}$else$$package$$endif$.impl.$if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Command.Hello;
import $if(mavenArchetype.truthy)$\${package}.\${serviceName}$else$$package$$endif$.impl.$if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Command.UseGreetingMessage;
import $if(mavenArchetype.truthy)$\${package}.\${serviceName}$else$$package$$endif$.impl.$if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Event.GreetingMessageChanged;

/**
* This is an event sourced entity. It has a state, {@link $name;format="Camel"$State}, which
* This is an event sourced entity. It has a state, {@link $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$State}, which
* stores what the greeting should be (eg, "Hello").
* <p>
* Event sourced entities are interacted with by sending them commands. This
Expand All @@ -29,13 +29,13 @@
* This entity defines one event, the {@link GreetingMessageChanged} event,
* which is emitted when a {@link UseGreetingMessage} command is received.
*/
public class $name;format="Camel"$Entity extends PersistentEntity<$name;format="Camel"$Command, $name;format="Camel"$Event, $name;format="Camel"$State> {
public class $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Entity extends PersistentEntity<$if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Command, $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Event, $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$State> {
/**
* An entity can define different behaviours for different states, but it will
* always start with an initial behaviour. This entity only has one behaviour.
*/
@Override
public Behavior initialBehavior(Optional<$name;format="Camel"$State> snapshotState) {
public Behavior initialBehavior(Optional<$if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$State> snapshotState) {
/*
* Behaviour is defined using a behaviour builder. The behaviour builder
* starts with a state, if this entity supports snapshotting (an
Expand All @@ -46,7 +46,7 @@ public class $name;format="Camel"$Entity extends PersistentEntity<$name;format="
* Otherwise, the default state is to use the Hello greeting.
*/
BehaviorBuilder b = newBehaviorBuilder(
snapshotState.orElse(new $name;format="Camel"$State("Hello", LocalDateTime.now().toString()))
snapshotState.orElse(new $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$State("Hello", LocalDateTime.now().toString()))
);

/*
Expand All @@ -67,7 +67,7 @@ public class $name;format="Camel"$Entity extends PersistentEntity<$name;format="
b.setEventHandler(GreetingMessageChanged.class,
// We simply update the current state to use the greeting message from
// the event.
evt -> new $name;format="Camel"$State(evt.message, LocalDateTime.now().toString())
evt -> new $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$State(evt.message, LocalDateTime.now().toString())
);

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package $package$.impl;
package $if(mavenArchetype.truthy)$\${package}.\${serviceName}$else$$package$$endif$.impl;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
Expand All @@ -11,27 +11,27 @@
import lombok.Value;

/**
* This interface defines all the events that the $name;format="Camel"$Entity supports.
* This interface defines all the events that the $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Entity supports.
* <p>
* By convention, the events should be inner classes of the interface, which
* makes it simple to get a complete picture of what events an entity has.
*/
public interface $name;format="Camel"$Event extends Jsonable, AggregateEvent<$name;format="Camel"$Event> {
public interface $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Event extends Jsonable, AggregateEvent<$if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Event> {
/**
* Tags are used for getting and publishing streams of events. Each event
* will have this tag, and in this case, we are partitioning the tags into
* 4 shards, which means we can have 4 concurrent processors/publishers of
* events.
*/
AggregateEventShards<$name;format="Camel"$Event> TAG = AggregateEventTag.sharded($name;format="Camel"$Event.class, 4);
AggregateEventShards<$if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Event> TAG = AggregateEventTag.sharded($if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Event.class, 4);

/**
* An event that represents a change in greeting message.
*/
@SuppressWarnings("serial")
@Value
@JsonDeserialize
final class GreetingMessageChanged implements $name;format="Camel"$Event {
final class GreetingMessageChanged implements $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Event {
public final String name;
public final String message;

Expand All @@ -43,7 +43,7 @@ final class GreetingMessageChanged implements $name;format="Camel"$Event {
}

@Override
default AggregateEventTagger<$name;format="Camel"$Event> aggregateTag() {
default AggregateEventTagger<$if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Event> aggregateTag() {
return TAG;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package $if(mavenArchetype.truthy)$\${package}.\${serviceName}$else$$package$$endif$.impl;

import com.google.inject.AbstractModule;
import com.lightbend.lagom.javadsl.server.ServiceGuiceSupport;

import $if(mavenArchetype.truthy)$\${package}.\${serviceName}$else$$package$$endif$.api.$if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Service;

/**
* The module that binds the $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Service so that it can be served.
*/
public class $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Module extends AbstractModule implements ServiceGuiceSupport {
@Override
protected void configure() {
bindService($if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$Service.class, $if(mavenArchetype.truthy)$\${serviceClassName}$else$$name;format="Camel"$$endif$ServiceImpl.class);
}
}
Loading

0 comments on commit 3de958f

Please sign in to comment.