Archive

Posts Tagged ‘eclipse’

Gradle and buildSrc and eclipse

I have been playing round with gradle and grails v3, there are a couple of things you need to know about gradle and eclipse. Especially if your going to write your extensions for gradle tasks etc.

one when you create a default gradle project in eclipse it doesn’t automatically build a buildSrc directory for you. But if you add one, gradle recognises this directory and tries to build the groovy/java files you might provide.

Moreover eclipse doesn’t recognise any such directory by itself and so the source code validation etc won’t really work. This is a bother if your want to use the IDE as your natural environment, which most do i suspect.

the default file structure you need to add is buildSrc/src/main/groovy or buildSrc/src/main/java respectively. Whilst gradle will build code in these folders, the IDE will treat this as a standard non source folder structure by default.

so if you try and add a class – it does what the IDE normally does and takes your class and the package and creates the file under the standard /src/main/groovy. I then had to move the it from there to the buildSrc sub directory.

The fix for this is to edit your build.gradle and explicitly add the buildSrc tree to the source sets and refresh all your dependencies (right click your project in explorer, and select gradle/refresh dependencies

sourceSets {
	main {
		java {
			srcDir 'src/main/java'
			srcDir 'src/main/groovy'
			srcDir 'buildSrc/src/main/java'
			srcDir 'buildSrc/src/main/groovy'
		}
		resources {
			srcDir 'src/resources'
			srcDir 'buildSrc/src/resources'
		}
	}
}

This will add the directory as a source folder in the project for you, and you can now create classes explicitly in these the new buildSrc folders.

Gradle automatically applies a default ‘build.gradle’ for the :buildSrc project if you don’t provide one which assumes the following defaults for you

apply plugin: 'groovy'
dependencies {
    compile gradleApi()
    compile localGroovy()
}

if you require additional flexibility you can provide your own in buildSrc/buildGradle, which is additive to this default, for example you might add

repositories {
    mavenCentral()
}

dependencies {
    testCompile 'junit:junit:4.11'
}

see section 60 – Organising build logic in the user guide.

However, this not all you have to do. As your going to add your plugin or code in buildSrc you need to import the gradle api classes. like this for a simple dummy class

 
package com.softwood.tasks
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction

class WillsTask extends DefaultTask {
	
	WillsTask () {
		group = 'Wills tasks'

		description = "one of wills tasks"
	}
	
	@TaskAction
	void start () {
		println "hello from Wills task"
	}

}

as the in buildSrc/src/main/groovy is now a source folder this throws allsort of warnings about not seeing the imports, (gradle my apply a default for you – but the IDE wont recognise that). So you have to go back to the build.gradle and add a compile dependency, like this

dependencies {
	compile gradleApi()
...
[/sourecode]

and refresh your dependencies again.  This gets the missing classes and adds them to your eclipse library 'Gradle Dependencies'

your buildSrc tree should now compile with no warnings and you can import your task definitions and use them in your build.gradle like this


import com.softwood.tasks.WillsTask

apply plugin: 'groovy'
apply plugin: 'eclipse'

....
task will (type : WillsTask) {
	doLast() {
		println "hello will"
	}
}

This now seems to work fine except that your main project dependencies now includes the gradleApi() classes. I haven’t figured how to setup dependencies just for the :buildSrc project and no other and keep them separated from the normal stuff in your project

within your build.gradle you can include a buildscript closure like this


buildscript {
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath gradleApi()
		classpath localGroovy()
		
	}
}

to add library dependencies to your :buildSrc classpath, in the above i’ve added the gradleApi() for example and the local groovy for the build (same as the implicit defaults). This works – but if the remove the ‘compile gradleApi()’ from your main dependencies your buildSrc folder will show failure to compile (as you have removed the libraries from ‘Gradle Dependencies’). But if you run the task ‘will’ it will all work.

I think this a deficiency with the interaction with eclipse and i need to query stack overflow/post a feature to get that resolved.

Advertisements
Categories: gradle Tags: , ,

Proxy Management for Gradle, Eclipse and Grails…

March 19, 2014 1 comment

This is an ever thorny subject and scope for much pain for users including myself

So thought i’d catalog the best way to set this up when working behind a firewall

starting with
Gradle

Best way for gradle is to add gradle.properties file into into the root of your project or into into USER_HOME/.gradle (user wide config)

basic attributes you may need to set are gradle.properties

systemProp.http.proxyHost=www.somehost.org
systemProp.http.proxyPort=8080
systemProp.http.proxyUser=userid
systemProp.http.proxyPassword=password

then refresh the dependencies (say from within your eclipse build with Gradle project support enabled)

Grails Projects

For Grails projects (v2 onwards) you can creat a ProxySettings.groovy file in the “${userHome}/.grails/scripts/ProxyConfig.groovy” directory and refresh the grails dependencies (note: this is global to all projects) where the file format is like this

MyProxy=['http.proxyHost':'xx.xx.xx.xx', 'http.proxyPort':'8080', 'http.proxyUser':'', 'http.proxyPassword':'', 'http.nonProxyHosts':'my_maven_server']
currentProxy='MyProxy'

alternatively you are supposed to be able run the following commands on grails

grails add-proxy proxyConf –host=proxy-server –port=4300
–username=guest –password=guest
grails set-proxy proxyConf

though i’m not certain that i have had this work in call cases but adding the file seems to.

lastly if that doesn’t work – you can try and edit your projects /conf/BuildConfig.groovy and add the following to that file

System.properties.putAll([
   "http.proxyHost": "myproxy.hostname.com",
    "http.proxyPort": "8080",
    "http.proxyUserName": "myUser",
    "http.proxyPassword": "myPass"
])

Eclipse itself

best i have found Eclipse itself is to add the proxy in the eclipse.ini file (or GGTS.ini etc ) with your eclipse build

you need to be careful – there can be no spaces round the definition else Eclipse doesn’t seem to read it correctly

You need to specify the following options below the -vmargs option in the .ini file, each on a separate line and spare whitespace at the end etc
-Dhttp.proxyPort=8080
-Dhttp.proxyHost=www.somehost.org
-Dhttp.proxyUser=mydomain\myusername
-Dhttp.proxyPassword=mypassword
-Dhttp.nonProxyHosts=localhost|127.0.0.1

and if this doesnt work you can try adding
-Dorg.eclipse.ecf.provider.filetransfer.excludeContributors=org.eclipse.ecf.provider.filetransfer

and try again.

Good luck

eclipse juno wtp servers and tomcat, get server 404 error …

March 23, 2013 Leave a comment

gawd damn waste of an hour – hope this saves you that someday

with eclipse juno, when you create a new server for apache tomcat (i was using tomcat7) it fails to copy the tomcat ROOT folder into the eclipse config area in <eclipse>\.metadata\.plugins\org.eclipse.wst.server.core\tmp<num>\wtpwebapps

so when you start the server and try to connect to port 8080 (default) you get a http 404 error.

That tells you at least that the server is up and working !

so now you have to go to your downloaded tomcat installation and copy the ROOT folder and paste into the file above in your eclipse config and restart.

then you have to copy the manager, docs etc web apps and copy them into the

<eclipse>\.metadata\.plugins\org.eclipse.wst.server.core\tmp<num>\webapps folder

now when you connect to localhost:8080 you get your default tomcat pages.

this site was what got me straightened out – i hope they fix this is eclipse its a bit rubbish and for new people starting out very disheartening when they cant get it to work

http://www.coreservlets.com/Apache-Tomcat-Tutorial/tomcat-7-with-eclipse.html

Categories: Eclipse Juno, Tomcat Tags: , ,
%d bloggers like this: