Is it possible to know which command is being executed from the BuildConfig.groovy file, in order to change build config depending on the command? For example, if 'schema-export' then do not include 'Foo' library and so on?
I'm using Eclipse and I am running rails cmd from eclipse.
EDIT: after trying out Shashank solution I added details on the fact that I am using Eclipse.
By printing the 'sun.java.command' property I think that my Eclipse installation(Indigo Service Release 2 + Grails IDE 3.5 plugin) is rewriting the command launched to
org.codehaus.groovy.grails.cli.support.GrailsStarter --main org.grails.ide.eclipse.longrunning.process.GrailsProcess --conf Y:\grails-2.4.4\/conf/groovy-starter.conf --classpath /C:/Program Files/eclipse/configuration/org.eclipse.osgi/bundles/1764/1/.cp/;/C:/Program Files/eclipse/configuration/org.eclipse.osgi/bundles/1766/1/.cp/ --is14
I looked upon it and yes, it is absolutely possible.
Inside anywhere in BuildConfig, you can write something like this:
String command = System.getProperty("sun.java.command")
if (command.contains("run-app")) {
compile (":hibernate:3.6.10.18") // example to install hibernate while running the app
} else if (command.contains("test-app")) {
compile (":hibernate:3.6.10.14") // some other version for test cases
}
Or for your example:
compile (":some-plugin:2.3") {
if (command.contains("export-schema")) {
excludes "foo"
}
}
That property will give you output like this:
org.codehaus.groovy.grails.cli.support.GrailsStarter --main org.codehaus.groovy.grails.cli.GrailsScriptRunner --conf /home/user/.gvm/grails/current/conf/groovy-starter.conf --classpath --offline run-app
Related
Configuration
Following the Plugins and Multi-Project Builds section at the Grails 3.2.11 manual, its suppose that I can setup a Multi-project with the next commands in a terminal:
echo "Creating the root folder..."
mkdir test-multi-project
cd test-multi-project
echo "Creating the settings.gradle file..."
echo "include 'myapp', 'myplugin'" >> settings.gradle
echo "Creating the Grails application..."
grails create-app myapp
echo "Creating the Grails plugin..."
grails create-plugin myplugin
echo "Configuring the dependency between the application and the plugin..."
echo "grails { plugins { compile project(':myplugin') } }" >> myapp/build.gradle
echo "Executing the Grails application..."
cd myapp
grails run-app
Error
However, when I tried those commands for create and configure the Grails Application and the Plugin the grails run-app command throws next error:
FAILURE: Build failed with an exception.
* Where:
Build file '~/test-multi-project/myapp/build.gradle' line: 61
* What went wrong:
A problem occurred evaluating root project 'myapp'.
> Project with path ':myplugin' could not be found in root project 'myapp'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
CONFIGURE FAILED
Total time: 4.835 secs
| Error Error initializing classpath: Project with path ':myplugin' could not be found in root project 'myapp'. (Use --stacktrace to see the full trace)
Additional Information
I already tested the above commands using Grails 3.2.8, 3.2.9, 3.2.10 and 3.2.11 and the code throws the same error.
On the other hand, I tested the above commands using Grails 3.2.3, 3.2.5, and 3.2.7 and the project is executed fine. Also, the Grails landing page shows that 'mypluin' is been used by the application.
Note, I am using sdk to handle the Grails versions. The commands were executed using Java 1.7 and Yosemite:
Groovy: 2.4.7
Ant: Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM: 1.7.0_141 (Azul Systems, Inc. 24.141-b11)
OS: Mac OS X 10.10.5 x86_64
Question:
I am wondering what else I need to do or what I am doing wrong in order to make this code works on Grails 3.2.11
Thanks in advance.
Jeff Brown fixed the above issue removing the multi-project-test2/myapp/settings.gradle file and adding the next line to the multi-project-test2/settings.gradle file:
project(':myapp').name = 'myapp'
As you can see at the next GitHub's commit: https://github.com/esalomon/multi-project-test2/commit/d92c3fbc67156bcd1af9f47dc6f2984534154bad
After the above updated the multi-project-test2 can be downloaded and it will work fine.
It is likely to have gone wrong at around this point:
echo "grails { plugins { compile project(':myplugin') } }" >> myapp/build.gradle
That segment needs to be added to existing block of that file not a new block (not that I have tested)
What happens when you follow the steps manually ? does it work that way and if so have you thought of doing a diff across files that changes to see what is different.
You are providing the script which is great and maybe someone else wishes to run through it but i suspect it is what has already been pointed out.
You should look into ed Something like this example:
I am using a grails 3.2.8 app which in the dependency segment ends like this:
runtime "com.h2database:h2"
testCompile "org.grails:grails-plugin-testing"
testCompile "org.grails.plugins:geb"
testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
Now if I execute
test328$ ed -s build.gradle <<EOF >/dev/null
g/^ testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
a
compile project(':myplugin')
.
w
q
EOF
you can now see:
tail -n20 build.gradle
runtime "com.h2database:h2"
testCompile "org.grails:grails-plugin-testing"
testCompile "org.grails.plugins:geb"
testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
compile project(':myplugin')
}
The new entry added on. You will need to find a pointer in your existing file generated by grails and use like above that pointer to add your entry
The ed script was used to demo this since obviously it wasn't clear enough previously
I would like to:
use the Frege programming language to write a simple "Hello World" piece of code,
then using the Frege compiler generating the equivalent Java source code,
then building an executable Jar file to run from the command line,
all the previous steps should be "controlled" by Gradle.
I am able to generate the source code (items 1. and 2. from the previous list), but I am not able to specify a "package" structure of the Java source code in output, i.e. I can not see the package Java statement as the first line of code in the generate Java source code. I can specify to the Frege compiler where to put the generated code though (via the -d argument).
I think this is the reason why when building an executable Jar, then launching it, I am seeing similar errors (according to different attempts on Gradle tasks) e.g.: no main manifest attribute.
The Frege source code is saved in a file named HelloFrege.fr, the generated Java source code is in a file named HelloFrege.java (I verified the file contains the expected main method).
Here there's a version of the Gradle "Jar task":
//create a single Jar with all dependencies
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Hello Frege Jar Example',
'Implementation-Version': version,
'Main-Class': 'HelloFrege'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
Here there's another version of the Gradle "Jar" task:
jar {
manifest {
attributes 'Main-Class': 'HelloFrege'
}
}
How can I solve this problem? I would like to avoid to manually add the package reference to the automatically generated Java source code file.
If your module name in Frege is unqualified such as HelloWorld, you will not see the package statement generated in Java. The module name will become the class name and the package will be empty or default package.
If your module name is qualified such as foo.bar.HelloWorld, then foo.bar will be the package name and HelloWorld will be the class name in the generated Java source.
The rule is that the last part of the module name becomes the class name and the qualifiers form the package name in the generated Java source.
I am not sure what gradle can do for you in this regard, but without gradle, the following should at least be possible:
... build your jar, as before ...
jar -uvfe project-all.jar HelloFrege
java -jar project-all.jar # run it
This, of course, is just another way to create a manifest. If this works, then it would be time to investigate why gradle refuses to do it.
Postscriptum: After thinking another minute about what the problem might be, it occurs to me that you may think that the source file name/path has anything to do with the java package name. This is not so in Frege, though it is good practice to have the file path match the package name, and the file base name match the class name (just like in Java). In addition, to remove some confusion, use the module keyword in frege. As explained by Marimuthu, the Java package and class name is derived from the frege module name.
Example:
$ cat Foo.fr
module my.org.Baz where
...
$ java -jar fregec.jar -d bin Foo.fr
This generates the Baz class in package my.org, and creates the class file in bin/my/org/Baz.class
I am posting here my findings so far. The combination of Gradle commands that works for me is the following one (calling it from the command line typing gradle clean generateJavaSrcFromFregeSrc fatJar):
task generateJavaSrcFromFregeSrc {
ant.java(jar:"lib/frege3.21.586-g026e8d7.jar",fork:true) {
arg(value: "-j") // do not run the java compiler
arg(value: "-d")
arg(value: "src/main/java") // the place where to put the generated source code (paired to the -d argument)
arg(value: "src/main/frege/HelloFrege.fr")
}
}
jar {
manifest {
attributes 'Main-Class': 'org.wathever.HelloFrege'
}
}
task fatJar(type: Jar) {
from files(sourceSets.main.output.classesDir)
from files(sourceSets.main.output.resourcesDir)
//from {configurations.compile.collect {zipTree(it)}} // this does not include the autogenerated source code
baseName = project.name + '-fatJar'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
The manifest details need to be specified in the jar block of code, if I specify them in the task fatJar then when running the jar I get no main manifest attribute, in [...].
If I use just the block of code jar with the property from("$projectDir") { include 'lib/**'} to include the Frege jar, then I get errors like java.lang.ClassNotFoundException (I think because the Jar is included as it is and not as a set of .class files).
The folder src/main/java/org/wathever needs to be there before running Gradle (additional info: the Maven convention prefix src/main/java with as a suffix the "Java package" as specified inside the HelloFrege.fr source code: module org.wathever.HelloFrege where)
Some useful details I found:
How to build a fat Jar
Another how to build a fat Jar
An "Hello Frege" example without the Gradle management
The Gradle documentation on how to use the Jar task
I want to slowly migrate to a better build and dependency resolution process, we are currently using Ant with local file dependencies.
We chose to migrate to gradle ,so as a first step I would like to simply run my current ant build from a gradle sript. But i dont know how to pass the -lib classpath to ant. Im getting errors of missing dependencies.
This is my gradle.build:
apply plugin: 'java'
task someProperties {
ext.LIBS_CATW = "backend/java-src/lib"
ext.LIB_SERVLET = "/usr/local/apache-tomcat-7.0.32/lib/servlet-api.jar"
}
dependencies {
compile files('/usr/local/apache-tomcat-7.0.32/lib/servlet-api.jar')
compile fileTree(dir: 'backend/java-src/lib', include: '*.jar')
}
I use this shell script to run ant from command line.
#!/bin/bash
export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home"
ROOT_DIR="/Users/poolebu/catwizardMultitenant/catwizard/catwBackend/branches/branchSpringSecurity/"
DIR_MT="$ROOT_DIR"
LIBS_CATW="$ROOT_DIR/backend/java-src/lib"
LIB_SERVLET="/usr/local/apache-tomcat-7.0.32/lib/servlet-api.jar"
ANT_TARGET="allButFlex"
cd $DIR_MT
ant -lib $LIBS_CATW -lib $LIB_SERVLET $ANT_TARGET
This is one of the many dependency errors I´m getting
[ant:javac] /Users/poolebu/catwizardMultitenant/catwizard/catwBackend/branches/branchSpringSecurity/backend/java-src/catw-common/src/com/bamboo/common/factory/SpringFactory.java:5: error: package flex.messaging does not exist
[ant:javac] import flex.messaging.FlexFactory;
[ant:javac] ^
I have a multi-project gradle build that has several Grails app.
One of these app contains many artefacts (controllers, services, groovy code, etc...).
When I run it using the gradle grails-run-app command, I get the following error:
| Error Server failed to start: PermGen space (Use --stacktrace to see the full trace)
We tried to add to buildConfig.groovy the following
disable.auto.recompile=true
and add the following to the gradle.properties
-XX:MaxPermSize=1G -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled
But the error remain
I am using:
Grails 2.3.1, Gradle 1.8 and grails-gradle-plugin 2.0.0-SNAPSHOT
Any ideas ?
Eduardo
Add the following lines to the begin of your gradle.build
import org.grails.gradle.plugin.GrailsTask
tasks.withType(org.grails.gradle.plugin.GrailsTask) { Task t ->
t.jvmOptions {
jvmArgs '-Xms32m -Xmx1524m -XX:PermSize=32m -XX:MaxPermSize=512m -XX:+UseCompressedOops -XX:+UseConcMarkSweepGC -XX:+UseParNewGC'.split(' ')
}
}
It runs every grails task with the specified jvm args.
Check this link
In our project there is a requirement like force all user to use mvn for running the grails application. So I have a task assigned for it like disable the run-app support or override run-app command so when other guy try to run application using grails runApp it will show some message and should exit. Can any body help please.
HI I resolved it by doing following step:
I created _Events.groovy by executing "grails create-script _Events". By doing this it will create _Events.groovy in scripts directory and in eventCompileStart I have written following code and it works fine for me.
eventCompileStart = {
if( BuildSettingsHolder.settings.grailsHome){
println"""
*********************************************************
* "grails run-app" support is disabled *
* *
* Try to run "mvn clean grails:run-app" *
* *
*********************************************************
"""
System.exit(0)
}
}
I hope it will help some one.