Build Scala Play app using different config file - scala

I have a Scala Play application builded using sbt.
When I build the package using sbt dist command, this will result in an archive deployed in /target/universal path. The problem is that I have 3 configuration files:
application.conf
dev.conf
prod.conf
If I try to run sbt dist -Dconfig.resource=prod.conf, I get an error:
[error] Expected ID character
[error] Not a valid command: prod (similar: stopProd, runProd, reload)
[error] Expected project ID
[error] Expected configuration
[error] Expected ':' (if selecting a configuration)
[error] Expected key
[error] Not a valid key: prod (similar: products, projectId, project-id)
[error] prod.conf
Even if I use quotes, or -Dconfig.file, the result is the same. Where am I wrong? thanks
late edit:
sbt dist will build the bin/*.bat files, but the rest of files will remain unbuilded. So, I think if I go for this .zip solution, the package will be the same for all environment types (dev, stage, prod), but I can choose the config file when I run the command to start application:
for example, for prod I can use:
nohup ./app-name-1.0/bin/app-name -J-Xms8g -J-Xmx8g -Dconfig.resource=prod.conf -Dhttp.port=7777</dev/null>> logfile.log 2>&1 &
where -Dconfig.resource=prod.conf make what I want in this post.

the dist command does not take a config argument , therefore you are receiving the error not a valid command.
You can use a system property to specify a config file https://www.playframework.com/documentation/2.8.x/ConfigFile.
This allows you to have a different file loaded for your production environment

Found a tutorial on scala-sbt.org for sbt-native-packager, but got strange errors.
For moment, the only solution to use a different config file for a sbt project builded as .bat file is to specify the config file for starting command, how I specified above (-Dconfig.resource=prod.conf):
nohup ./path -J-Xms8g -J-Xmx8g -Dconfig.resource=prod.conf -Dhttp.port=7777</dev/null>>
So, first step is to use sbt dist to create the zip package without matter about config file. Then, unzip the file on server, and use command to start the application.

Related

Running sbt tasks from commandline without plugin.sbt

How to run an sbt build with task from a plugin that is not defined in plugins.sbt?
In maven it's as easy as:
mvn groupId:artifactId:1.2.3:goal
e.g. mvn org.owasp:dependency-check-maven:7.1.1:check
In sbt I get:
$ sbt net.vonbuchholtz:sbt-dependency-check:4.1.0:check
...
[error] Expected ID character
[error] Not a valid command: net (similar: set, new, inspect)
[error] Expected project ID
[error] Expected configuration
[error] Expected ':'
[error] Expected key
[error] Not a valid key: net (similar: test, name, run)
[error] net.vonbuchholtz:sbt-dependency-check:4.1.0:check
[error] ^
You have to have plugin added to sbt to be able to call it.
If you don't want to add it to the project you can add it globally:
// Put things into
// ~/.sbt/1.0/plugins/plugins.sbt
// Actually, you can name the file differently as long as it's in
// ~/.sbt/1.0/plugins/
// and ends with .sbt, sbt will load all files ending with .sbt
// from there.
addSbtPlugin("net.vonbuchholtz" % "sbt-dependency-check" % "4.1.0")
Alternatively, you can define some globally ignored file in .gitignore (assuming you are using git and don't want to accidentally commit things).
// ~/.gitconfig
[core]
excludesfile = ~/.gitignore_global
// ~/.gitignore_global
local.sbt
Then you can add all you want there e.g. local.sbt - you will be able to add some ad hoc changes to you repo and don't worry that they will be commited upstream.
As yet another option, you can put these plugin configs into some file e.g. ~/.extra_plugins.sbt and add them with a command line:
// ~/extra_plugins.sbt
addSbtPlugin("net.vonbuchholtz" % "sbt-dependency-check" % "4.1.0")
sbt --addPluginSbtFile="~/extra_plugins.sbt" dependencyCheck
As far as I can tell you cannot skip the step when you are creating the .sbt file. At best you could auto-generate it and populate it with a script:
// something like this
local tmp_sbt=`mktemp`
echo 'addSbtPlugin("net.vonbuchholtz" % "sbt-dependency-check" % "4.1.0")' >> "$tmp_sbt"
sbt --addPluginSbtFile="$tmp_sbt" dependencyCheck
rm "$tmp_sbt"

VSTS - build definition error in solution file

This is probably something simple that I am doing wrong as I am new to VSTS.
I have uploaded my existing .NET website project to VSTS and when I run a build definition in VSTS, I get an error in the solution file.
I can see what the problem is....the solution file has the pathway of the project on my PC in it...which is stored in inetpub/wwwroot/VSProjects on my PC.
Is there something simple I am missing to avoid this? There is probably a certain way that a .net website should be uploaded to vsts to avoid this pathway error?
Thanks for any help...
Here are the build solution log errors -
2018-01-19T16:34:14.3619604Z C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_compiler.exe -v /PropertyRegister -p ..\..\..\..\..\..\inetpub\wwwroot\VSProjects\PropertyRegister\ -u -f PrecompiledWeb\PropertyRegister\
2018-01-19T16:34:16.1411730Z ##[error]ASPNETCOMPILER(0,0): Error 1003: The directory 'd:\inetpub\wwwroot\VSProjects\PropertyRegister\' doesn't exist.
2018-01-19T16:34:16.1419278Z ASPNETCOMPILER : error 1003: The directory 'd:\inetpub\wwwroot\VSProjects\PropertyRegister\' doesn't exist. [d:\a\3\s\PropertyRegister\PropertyRegister.metaproj]
2018-01-19T16:34:16.1806460Z Done Building Project "d:\a\3\s\PropertyRegister\PropertyRegister.metaproj" (default targets) -- FAILED.
2018-01-19T16:34:16.1818301Z Done Building Project "d:\a\3\s\PropertyRegister\PropertyRegister.sln" (default targets) -- FAILED.
2018-01-19T16:34:16.1837158Z
2018-01-19T16:34:16.1837587Z Build FAILED.
2018-01-19T16:34:16.1873374Z
2018-01-19T16:34:16.1882800Z "d:\a\3\s\PropertyRegister\PropertyRegister.sln" (default target) (1) ->
2018-01-19T16:34:16.1884216Z (ValidateProjects target) ->
2018-01-19T16:34:16.1885795Z d:\a\3\s\PropertyRegister\PropertyRegister.sln.metaproj : warning MSB4121: The project configuration for project "PropertyRegister" was not specified in the solution file for the solution configuration "Release|Any CPU". [d:\a\3\s\PropertyRegister\PropertyRegister.sln]
2018-01-19T16:34:16.1886267Z
In VS Build task, you should specify the publishproj file instead of sln in Solution option of the VS Build task for your website project.
You can specify the detail relative path for the publishproj file or use wildcards to match the website project you want to build as below examples:

Custom deployment with sbt

I have a Scala application and i want to setup a deployment process result similar to one Akka sbt plugin gives, but i it requires project to be a microkernel. Sbt-assembly is a very cool plugin, but i want to have a free access to my config files, basically i want to have the following app structure:
/app/bin - start script bash file
/config - all my application .conf files
/deploy - my project .jar with classes
/lib - all my dependencies .jar files
/logs - log files
I we checked typesafe sbt-native-packager, it's better, but it could place my .conf file and logs out of jars. All those settings in SBT looks confusing to me, what can i do to with this?
Actually this is not hard to update akka-sbt-plugin to make it work in general, add this file to your project folder and somewhere in your build the following settings:
.settings(Distribution.distSettings: _*)
.settings(mappings in Compile in packageBin ~= { _.filter(!_._1.getName.endsWith(".conf")) })
The first line adds all dist settings to your project and the second one excludes all .conf files from your .jar and reads them from config folder.
Now you have to commands: dist - creates a folder with a structure you've discribed and zipDist which packs it all into .zip file, later you can add this to you publish setting:
addArtifact(Artifact(someName, "zip", "zip"), zipDist)

How to configure build.sbt so that xsbt-web-plugin a creates war file without compression?

I am using Scala 2.10.1 with sbt to package my webapp as a war file.
For the purpose of efficient rsync deltas, I'd like to have the war packaged as a .war file, but without zip compression. I just need to know how to configure my build for this.
UPDATE:
All these plugin docs assume all this knowledge of how the syntax works and how to combine tasks into a new task, etc. I can't even tell how to create a new task that does package then command. None of the answers so far have said specifically, "here's what you do.."
Just to be clear, this is all I'm asking for:
I need a Task "packnozip" that does this:
1) run "package"
2) run shell commands:$ mkdir ./Whatever
$ pushd ./Whatever
$ jar xvf ../Whatever.war
$ popd
$ mv ./Whatever.war ./Whatever.war.orig
$ jar cvM0f ./Whatever.war -C ./Whatever .
So what i'm saying is i want to type "packnozip" into the sbt console and have it do #1 then #2.
For now i'm just manually doing #2 which seems silly if it can be automated.
Also watching a 30MB file get completely resent by rsync b/c it is not diffable seems quite silly when a 34MB uncompressed file is only 13% more data, and takes a fraction of second to send b/c of efficient diffs, not to mention "-z" will compress the transfer anyways.
If you have your war file unzipped in a directory you can:
zip -r -0 project.war project/
That should be zero compression. In case you don't see those options, this is my setup:
[node#hip1 dev]$ zip -v
Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
This is Zip 3.0 (July 5th 2008), by Info-ZIP.
Which, you could execute as a run task I believe, after the war is packaged.
UPDATE 1
I believe this is the best way to achieve your needs:
http://www.scala-sbt.org/release/docs/Detailed-Topics/Process
val exitcode = "zip -r -0 project.war project/"!
However, if you need to work from a specific directory (Please see Update 2 below):
Modified this to execute within directory but place .war above directory. The path (2nd) argument should include the directory, so that the zip is performed inside of it:
Process("zip" :: "-r" :: "-0" :: "../project.war" :: "." :: Nil, "/path/to/project/") !
Here's another SO question on the ProcessBuilder that may help as well:
How does the “scala.sys.process” from Scala 2.9 work?
(Note: you don't need to import scala.sys.process._)
UPDATE 2
For readers in the future, please note that zipping the project directory itself will not work, one needs to perform the zip of the war inside the directory by using pushd, putting the resulting war outside of the directory as mentioned by the OP in the comments below this answer. As Orange80 mentioned:
pushd ./project && zip -r -0 ../project.war ./ && popd
UPDATE 3
Check out this, it may do exactly what you need, with a 0 for options to specify no compression:
https://github.com/sbt/sbt-onejar
a plugin that lets you create a single executable jar, which, with options (for example "0" as in a command like "jar 0f blah.jar blah/") can be made I think as you mentioned in the comments below to create the jar file without compression.
For usage I found this on SO:
SBT one-jar plugin
And also, if it needs to be modified, it's a pretty reasonable example of a plugin as well, which if you drop it in your home ~/.sbt/plugins it will be global and can be used in your build in the fashion noted in the SO answer above. I hope that helps at least a little bit/
There is no way to do this directly via sbt configuration, since sbt assumes that any files within zip and jar artifacts should be compressed.
One workaround is to unzip and re-zip (without compression) the war file. You can do this by adding the following setting to your project (e.g. in build.sbt):
packageWar in Compile <<= packageWar in Compile map { file =>
println("(Re)packaging with zero compression...")
import java.io.{FileInputStream,FileOutputStream,ByteArrayOutputStream}
import java.util.zip.{CRC32,ZipEntry,ZipInputStream,ZipOutputStream}
val zis = new ZipInputStream(new FileInputStream(file))
val tmp = new File(file.getAbsolutePath + "_decompressed")
val zos = new ZipOutputStream(new FileOutputStream(tmp))
zos.setMethod(ZipOutputStream.STORED)
Iterator.continually(zis.getNextEntry).
takeWhile(ze => ze != null).
foreach { ze =>
val baos = new ByteArrayOutputStream
Iterator.continually(zis.read()).
takeWhile(-1 !=).
foreach(baos.write)
val bytes = baos.toByteArray
ze.setMethod(ZipEntry.STORED)
ze.setSize(baos.size)
ze.setCompressedSize(baos.size)
val crc = new CRC32
crc.update(bytes)
ze.setCrc(crc.getValue)
zos.putNextEntry(ze)
zos.write(bytes)
zos.closeEntry
zis.closeEntry
}
zos.close
zis.close
tmp.renameTo(file)
file
}
Now when you run package in sbt, the final war file will be uncompressed, which you can verify with unzip -vl path/to/package.war.

Why would Eclipse not be able to include a file when running a PHPUnit test?

I have the following class and unit test in a PHP project in Eclipse:
I know my unit test works as I can run it at the command line:
Now I want to run this test from Eclipse. I set up PHP Unit in Eclipse like this:
However, when I run the PHPUnit tests:
It tells me that it can't include the class file:
/usr/bin/php -c /var/folders/UA/UAv38snBHd0QMgEPMCmM9U+++TM/-Tmp-/zend_debug/session4910937990995915704.tmp -d asp_tags=off /Applications/eclipse/plugins/org.phpsrc.eclipse.pti.tools.phpunit_0.5.0.R20101103000000/php/tools/phpunit.php --log-junit /var/folders/UA/UAv38snBHd0QMgEPMCmM9U+++TM/-Tmp-/pti_phpunit/phpunit.xml /Volumes/data/domains/et/extjslayout/phpunittest/tests
PHP Warning: include_once(../Product.php): failed to open stream: No such file or directory in /Volumes/data/domains/et/extjslayout/phpunittest/tests/ProductTest.php on line 3
PHP Warning: include_once(): Failed opening '../Product.php' for inclusion (include_path='/usr/local/PEAR') in /Volumes/data/domains/et/extjslayout/phpunittest/tests/ProductTest.php on line 3
PHP Fatal error: Class 'Product' not found in /Volumes/data/domains/et/extjslayout/phpunittest/tests/ProductTest.php on line 9
Why would PHPUnit be able to find the class when run from the command line but not when run from Eclipse?
When you start something from the command line, the "current directory" has a well-defined meaning: It's the directory where you started the command.
In Eclipse, what is the "current directory"? It's probably the directory from which you started Eclipse or maybe the folder in which Eclipse is installed.
I haven't used PHP in Eclipse before but for other languages, I can set the current directory in the launch config somewhere. If that doesn't work, define a variable which points to your project and then use absolute paths (using that variable as a starting point).
Have same problem. Found only solution by creating tests with internal PHPUnit wizard like at this screenshot:
Source: HowTo create a Test Case Class from a PHP Class
But following investigate show that your test case file should contain reference to tested code for example like this: require_once 'C:\Apache2\htdocs\jobeet\src\Ibw\JobeetBundle\Utils\Jobeet.php';
Other experiments with plugin config not bringing luck. So in my opinion PHPUnit from PHP Tools not well developed plugin. Consider using MakeGood plugin as better alternative.