Skip to content

Commit

Permalink
Add property to configure dependency prefixing
Browse files Browse the repository at this point in the history
Signed-off-by: Iacopo Pace <[email protected]>
  • Loading branch information
iaco86 committed Apr 27, 2021
1 parent 0d20d45 commit 4def6d1
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 20 deletions.
4 changes: 3 additions & 1 deletion src/docs/asciidoc/30-plugin-configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ include::33-add-source-directories.adoc[]

include::34-compiler-options.adoc[]

include::35-add-distribution-files.adoc[]
include::35-add-distribution-files.adoc[]

include::36-prefixing-dependencies.adoc[]
14 changes: 14 additions & 0 deletions src/docs/asciidoc/36-prefixing-dependencies.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== Prefixing dependencies

By default, your project's distribution task will prefix all your dependencies as follows:

* Each jar will be prefixed with the library's group name - e.g. `com.typesafe.play-play_2.12-2.6.25.jar`
* Each sub-module's jar, in a multi-module project, will be prefixed with the module's name
The default behavior can be turned off using the `prefixDependencies` configuration.

[source,groovy]
.build.gradle
----
include::{samplesCodeDir}/play-2.4/groovy/build.gradle[tag=prefix-dependencies-off]
----
6 changes: 6 additions & 0 deletions src/docs/samples/play-2.4/groovy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ play {
injectedRoutesGenerator = true
}
// end::injected-routes-compiler[]

// tag::prefix-dependencies-off[]
play {
prefixDependencies = false
}
// end::prefix-dependencies-off[]
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package org.gradle.playframework.application

import org.gradle.playframework.PlayMultiVersionApplicationIntegrationTest
import org.gradle.playframework.fixtures.archive.ArchiveTestFixture
import org.gradle.playframework.fixtures.archive.JarTestFixture
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.TaskOutcome
import org.gradle.util.VersionNumber

import java.util.jar.Attributes

import static org.gradle.playframework.plugins.PlayApplicationPlugin.ASSETS_JAR_TASK_NAME
import static org.gradle.playframework.plugins.PlayRoutesPlugin.ROUTES_COMPILE_TASK_NAME
import static org.gradle.playframework.plugins.PlayTwirlPlugin.TWIRL_COMPILE_TASK_NAME
Expand Down Expand Up @@ -38,6 +35,7 @@ abstract class PlayDistributionApplicationIntegrationTest extends PlayMultiVersi
and:
verifyJars()
verifyStagedFiles()
verifyDependenciesPrefix()

when:
result = build("dist")
Expand All @@ -56,6 +54,7 @@ abstract class PlayDistributionApplicationIntegrationTest extends PlayMultiVersi
List<ArchiveTestFixture> archives() {
[ zip("build/distributions/main.zip"), tar("build/distributions/main.tar") ]
}

void verifyArchives() {
archives()*.containsDescendants(
"main/lib/${playApp.name}.jar",
Expand All @@ -66,6 +65,15 @@ abstract class PlayDistributionApplicationIntegrationTest extends PlayMultiVersi
"main/README")
}

protected boolean shouldPrefixDependencies() {
return true
}

final void verifyDependenciesPrefix() {
def dependencies = file("build/stage/main/lib/").listFiles().collect { it.name }
assert dependencies.any { it.startsWith("com.typesafe.play-play_") } == shouldPrefixDependencies()
}

void verifyStagedFiles() {
File stageMainDir = file("build/stage/main")
[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.gradle.playframework.application.dependencies

import org.gradle.playframework.application.PlayDistributionApplicationIntegrationTest
import org.gradle.playframework.fixtures.app.PlayApp
import org.gradle.playframework.fixtures.app.PlayAppWithDependenciesNoPrefix

class PlayDistributionAppWithDependenciesNoPrefixIntegrationTest extends PlayDistributionApplicationIntegrationTest {

@Override
PlayApp getPlayApp() {
new PlayAppWithDependenciesNoPrefix(playVersion)
}

@Override
protected boolean shouldPrefixDependencies() {
return false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.gradle.playframework.fixtures.app

import org.gradle.util.VersionNumber

class PlayAppWithDependenciesNoPrefix extends PlayApp {
PlayAppWithDependenciesNoPrefix(VersionNumber version) {
super(version)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed 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 controllers

<#if playVersion == "2.8" || playVersion == "2.7" || playVersion == "2.6">
import javax.inject._
import com.google.common.base.Strings
import play.api._
import play.api.mvc._

@Singleton
class Application @Inject() extends InjectedController {

def index = Action {
Ok(views.html.index(Strings.nullToEmpty("Your new application is ready.")))
}

def shutdown = Action {
Runtime.getRuntime().halt(0)
Ok("shutdown")
}
}
<#else>
import com.google.common.base.Strings
import play.api._
import play.api.mvc._

object Application extends Controller {

def index = Action {
Ok(views.html.index(Strings.nullToEmpty("Your new application is ready.")))
}

def shutdown = Action {
System.exit(0)
Ok("shutdown")
}
}
</#if>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@(message: String)

@main("Welcome to Play") {

<h1>@message</h1>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@(title: String)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>@title</title>
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
<script src="@routes.Assets.at("javascripts/hello.js")" type="text/javascript"></script>
</head>
<body>
@content
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<#if playVersion == "2.8" || playVersion == "2.7" || playVersion == "2.6">
plugins {
id 'org.gradle.playframework'
}

dependencies {
implementation "com.google.guava:guava:17.0"
implementation "com.typesafe.play:play-guice_2.12:2.6.15"
implementation "ch.qos.logback:logback-classic:1.2.3"
testImplementation "commons-lang:commons-lang:2.6"
}

play {
prefixDependencies = false
}

// repositories added in PlayApp class

<#else>
plugins {
id 'org.gradle.playframework'
}

dependencies {
implementation "com.google.guava:guava:17.0"
testImplementation "commons-lang:commons-lang:2.6"
}

// repositories added in PlayApp class

play {
prefixDependencies = false
}

</#if>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<#if playVersion == "2.8" || playVersion == "2.7" || playVersion == "2.6">
# Routes
# Home page
GET / @controllers.Application.index

GET /shutdown @controllers.Application.shutdown

# Map static resources from the /public folder to the /assets URL path
GET /assets/*file @controllers.Assets.at(path="/public", file)

<#else>
# Routes
# Home page
GET / controllers.Application.index

GET /shutdown controllers.Application.shutdown

# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)

</#if>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed 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.
*/

import org.junit._

class ApplicationSpec {
@Test
def passingTest() {
}
@Test
def passingTest2() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed 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.
*/

import org.junit._

class IntegrationSpec {
@Test
def passingTest() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: Test
number: 1234
items:
- name: item1
description: first item
- name: item2
description: second item
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.gradle.playframework.extensions;

import javax.inject.Inject;

import org.gradle.api.Action;
import org.gradle.api.JavaVersion;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.playframework.extensions.internal.PlayMajorVersion;

import static org.gradle.playframework.extensions.PlayPlatform.DEFAULT_PLAY_VERSION;
Expand All @@ -22,21 +25,25 @@
* javaVersion = JavaVersion.VERSION_1_9
* }
* injectedRoutesGenerator = true
* prefixDependencies = false
* }
* </pre>
*/
public class PlayExtension {

private final PlayPlatform platform;
private final Property<Boolean> injectedRoutesGenerator;
private final Property<Boolean> prefixDependencies;

@Inject
public PlayExtension(ObjectFactory objectFactory) {
this.platform = objectFactory.newInstance(PlayPlatform.class, objectFactory);
this.platform.getPlayVersion().convention(DEFAULT_PLAY_VERSION);
this.platform.getJavaVersion().convention(JavaVersion.current());
this.platform.getScalaVersion().convention(platform.getPlayVersion().map(playVersion -> PlayMajorVersion.forPlayVersion(playVersion).getDefaultScalaPlatform()));
this.injectedRoutesGenerator = objectFactory.property(Boolean.class);
injectedRoutesGenerator.convention(platform.getPlayVersion().map(playVersion -> !PlayMajorVersion.forPlayVersion(playVersion).hasSupportForStaticRoutesGenerator()));
this.injectedRoutesGenerator = objectFactory.property(Boolean.class)
.convention(platform.getPlayVersion().map(playVersion -> !PlayMajorVersion.forPlayVersion(playVersion).hasSupportForStaticRoutesGenerator()));
this.prefixDependencies = objectFactory.property(Boolean.class).convention(true);
}

/**
Expand Down Expand Up @@ -66,4 +73,13 @@ public PlayPlatform getPlatform() {
public Property<Boolean> getInjectedRoutesGenerator() {
return injectedRoutesGenerator;
}

/**
* Returns the property configuring if the dependencies for the distribution should be prefixed.
*
* @return Property configuring the renaming of dependencies
*/
public Property<Boolean> getPrefixDependencies() {
return prefixDependencies;
}
}
Loading

0 comments on commit 4def6d1

Please sign in to comment.