Intellij IDEA - Output path ...\project\target\idea-classes intersects with a source root. Only files that were created by build will be cleaned - scala

Using Intellij IDEA with Scala plugin.
When doing a Build -> Rebuild Project I get the following make warnings:
Output path ProjectRootFolder\project\target\idea-test-classes intersects with a source root. Only files that were created by build will be cleaned.
Output path ProjectRootFolder\project\target\idea-classes intersects with a source root. Only files that were created by build will be cleaned.
The project was generated with SBT gen-idea plugin.
The two output paths mentioned in the warnings are set as output path and test output path for the build module of the project under Project Structure -> Modules -> ProjectName-build -> Paths -> Use module compile output path.
Looking at the Sources tab for both the ProjectName module and ProjectName-build modules I saw that there is no place where ProjectRootFolder\project\target was marked as Source.

It seems the warnings were caused by the fact that the project and . folders were marked as Sources in the ProjectName-build module.
Since the SBT build module is not needed when using IDEA to build the project, one solution would be to generate the IDEA project without that module:
sbt gen-idea no-sbt-build-module
More details here: https://github.com/mpeltonen/sbt-idea/issues/180
UPDATE
Removing the build module is actually problematic since the build.scala file will show a lot of warnings because the required libraries would be missing.
A solution would be to unmark . and project from being Sources of the build module, which is also troublesome since it would need to be done after each gen-idea.
A better solution would be to use sbt to build the project instead of make. To achieve that remove the make before launch step in the IDEA run configuration and add a SBT products step instead.

I get the same warning and it didn't cause any problems so far.
Judging by this code it seems that they simply delete only files generated by IDE, otherwise they would want to delete everything in target directory. They are playing safe by checking if there could be any source files there:
// check that output and source roots are not overlapping
final List<File> filesToDelete = new ArrayList<File>();
for (Map.Entry<File, Collection<BuildTarget<?>>> entry : rootsToDelete.entrySet()) {
context.checkCanceled();
boolean okToDelete = true;
final File outputRoot = entry.getKey();
if (JpsPathUtil.isUnder(allSourceRoots, outputRoot)) {
okToDelete = false;
}
else {
final Set<File> _outRoot = Collections.singleton(outputRoot);
for (File srcRoot : allSourceRoots) {
if (JpsPathUtil.isUnder(_outRoot, srcRoot)) {
okToDelete = false;
break;
}
}
}
if (okToDelete) {
// do not delete output root itself to avoid lots of unnecessary "roots_changed" events in IDEA
final File[] children = outputRoot.listFiles();
if (children != null) {
filesToDelete.addAll(Arrays.asList(children));
}
else if (outputRoot.isFile()) {
filesToDelete.add(outputRoot);
}
}
else {
context.processMessage(new CompilerMessage(
"", BuildMessage.Kind.WARNING, "Output path " + outputRoot.getPath() + " intersects with a source root. Only files that were created by build will be cleaned.")
);
// clean only those files we are aware of
for (BuildTarget<?> target : entry.getValue()) {
clearOutputFiles(context, target);
}
}
}

Related

Gradle DSL - Eclipse Equivalent for IDEA Module Property

Good localtime,
I am in the process of updating legacy (4.8.1) Gradle build files for a big-McLarge-huge, multimodule project. We utilize an intellij.gradle file which has the following line (marked by comment):
idea {
module {
inheritOutputDirs = true // <-- HOW DO I DO THIS
downloadJavadoc = true
downloadSources = true
}
workspace.iws.withXml { provider ->
def node = provider.asNode()
def dynamicClasspath = node.component.find { it."#name" == "dynamic.classpath" }
if (dynamicClasspath != null) {
dynamicClasspath."#value" = "true"
}
}
From the 4.8.1 DSL docs:
If true, output directories for this module will be located below the
output directory for the project; otherwise, they will be set to the
directories specified by IdeaModule.getOutputDir() and
IdeaModule.getTestOutputDir().
Any ideas on what the Eclipse DSL equivalent of inheritOutputDirs? Should this be handled using the eclipseClasspath API? Right now everything is building fine, but the Eclipse Java builder is is flagging things.
References:
https://docs.gradle.org/4.8.1/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html
https://docs.gradle.org/4.8.1/dsl/org.gradle.plugins.ide.eclipse.model.EclipseClasspath.html
Usually this would have been picked up through sourceSets but I can't see what your project looks like...
If your subproject uses Gradle to generate sources into /build/cxf/generated-sources directory, then you can tell Eclipse via Gradle DSL to include that as a source folder like this:
plugins { id 'eclipse' }
eclipse.classpath.file.whenMerged {
// this is the brute-force approach; there is likely a better way to add a source folder
entries << new org.gradle.plugins.ide.eclipse.model.SourceFolder('build/cxf/generated-sources', null)
}
Once this is run (via gradle eclipseClasspath) you should see a build/cxf/generated-sources folder under your project node in the Package Explorer or Project Explorer. Sort of like this:
NOTE: This is untested because I don not have a sample project to work with.
There is more discussion here: How to add gradle generated source folder to Eclipse project?

download a zip from url and extract it in resource using SBT

I want to download a zip file (my database) from a URL and extract it in specific folder (e.g. resource). I want to do it in my project build sbt file.
What would be the appropriate way to do that?
I know that sbt.IO has unzip and download. I couldn't find a good example that uses download (those I found were not working).
Is there any sbt plugin to do this for me?
It's not clear when you want to download and extract, so I'm going to do it with a TaskKey. This will create a task you can run from the sbt console called downloadFromZip, which will just download the sbt zip and extract it to a temp folder:
lazy val downloadFromZip = taskKey[Unit]("Download the sbt zip and extract it to ./temp")
downloadFromZip := {
IO.unzipURL(new URL("https://dl.bintray.com/sbt/native-packages/sbt/0.13.7/sbt-0.13.7.zip"), new File("temp"))
}
This task can be modified to only run once if the path already exists:
downloadFromZip := {
if(java.nio.file.Files.notExists(new File("temp").toPath())) {
println("Path does not exist, downloading...")
IO.unzipURL(new URL("https://dl.bintray.com/sbt/native-packages/sbt/0.13.7/sbt-0.13.7.zip"), new File("temp"))
} else {
println("Path exists, no need to download.")
}
}
And to have it run on compilation, add this line to build.sbt (or project settings in Build.scala).
compile in Compile <<= (compile in Compile).dependsOn(downloadFromZip)

How to cherry pick ZIP contents with Gradle Distribution plugin?

My Gradle build currently produces the following directory structure under a build dir in my project root:
myapp/
src/
build.gradle
build/
docs/
groovydoc/* (all Groovydocs)
libs/
myapp-SNAPSHOT.jar
myapp-SNAPSHOT-sources.jar
reports/
codenarc/
main.html
test-results/* (JUnit test results)
I would like to add the distribution plugin (or anything that accomplishes my goals, really) to have Gradle produce a ZIP file with the following directory structure:
myapp-SNAPSHOT-buildreport.zip/
tests/
(JUnit tests from build/test-results above)
reports/
main.html (CodeNarc report from build/reports/codenarc above)
api/
(Groovydocs from build/docs above)
source/
myapp-SNAPSHOT-sources.jar (from build/libs above)
bin/
myapp-SNAPSHOT.jar (from build/libs above)
After reading the plugin's documentation, I can't tell how to configure it to suit these needs. Its obvious that I need to run gradle distZip, but as to how to actually configure it to produce the desired directory structure, it doesn't seem to provide any documentation/examples. Any ideas?
Note: The JAR's version is obviously SNAPSHOT, and is passed into the Gradle build with a -Pversion=SNAPSHOT command-line argument.
The Gradle Distribution plugin automatically has defaults (the problem is that the docs do not tell us the defaults, but the Gradle project default structure is what is assumed) so if your Gradle project is fairly straightforward and already using src/main/groovy or src/main/java, you typically just need to...
Use the CopySpec reversing pattern of giving your into{} (makes a dir) containing the contents of from{} , rather than the reverse, like so:
apply plugin: 'groovy'
apply plugin: 'eclipse'
apply plugin: 'application'
distributions {
main {
baseName= 'vz_sde_dm'
contents {
into('bin/config') {
from 'config'
}
into('lib/samples') {
from 'samples'
}
}
}
}
Notice I did not need to define a from{} for my contents{}, but only into{}s that's because I am already using the default Gradle Groovy project layout and only added 2 extra folders (config & samples) under my project in Eclipse but needed those 2 folders to land into a slightly different hierarchy for my Distribution compared to my regular build folder layout.
I would probably not use the distribution plugin and instead just create a new custom Zip task. It would look something like this:
task buildreportZip(type: Zip, dependsOn: build) {
classifier = 'buildreport'
from('build/test-results') {
into 'tests'
}
from('build/reports/codenarc') {
into 'reports'
}
from('build/docs') {
into 'api'
}
from(sourcesJar) { // or whatever you source jar task name is
into 'source'
}
from(jar) {
into 'bin'
}
}
I was trying to make a custom layout also, and had real trouble figuring out how to exclude the project output from build/libs from the yourProject.zip/yourProject/lib directory (and excluding things in general) and putting it instead into yourProject.zip/yourProject.
After quite a few hours across multiple days of searching and poking around in the API I finally found something that worked using actual configurations of the Distribution and underlying CopySpec (documented here and here, respectively for Gradle 5.6.1, you can just replace 5.6.1 with current in the URL to get the most recent API docs, 5.6.1 just happens to be the version I'm using):
distributions {
main {
baseName = appName
contents {
filesMatching("**/${appName}.jar", {
if (it.getPath().contains('/lib/')) {
it.setPath(it.getPath().replace('lib/', ''))
}
})
into('config') {
exclude(['server.crt', 'spotbugs-exclusion-filters.xml'])
from 'src/main/resources'
}
}
}
}
For exclusions the only thing that worked was matching on a glob pattern and specifying the correct action (to copy it to the root dist directory instead of root/lib) with a Closure through the filesMatching method of the main distribution's content CopySpec. You can see also how destination for configs is changed from the root to the root/config directory. Thanks so Thad's answer for helping to guide me to the correct build configuration, also.

Gradle Api Sources and Doc when writing Gradle Plugins

The question:
How do I get the sources and javadoc/groovydoc for the gradle-api code integrated into an Eclipse project?
Background:
I'm using Gradle to build a Gradle-plugin that I'm writing. I'm using Eclipse as an IDE for this project and my Gradle script for building this plugin is using the 'Eclipse' plugin to generate my Eclipse project. Also, I'm using Spring's Gradle plugin for Eclipse which grabs all my dependencies from my build.gradle file.
The dependencies block for this Gradle script has
dependencies {
compile localGroovy()
compile gradleApi()
// I want something like: 'compile gradleApiSources()' here
// I want something like: 'compile gradleApiDoc()' here as well
}
Justification:
As I'm learning to write Gradle plugins, it would be helpful to be able to see the documentation and even implementation for Gradle to help me learn what I'm doing.
This works for me in Eclipse:
plugins.withType(EclipsePlugin) {
plugins.withType(JavaBasePlugin) {
eclipse {
classpath {
file {
whenMerged { classpath ->
String gradleHome = gradle.getGradleHomeDir()
.absolutePath
.replace(File.separator, '/')
String gradleSourceDirectory = "${gradleHome}/src"
classpath.entries.each { entry ->
if (entry in org.gradle.plugins.ide.eclipse.model.AbstractLibrary
&& entry.library.path.contains('generated-gradle-jars')) {
entry.sourcePath =
new org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory()
.fromPath(gradleSourceDirectory)
}
}
}
}
}
}
}
}
Make sure that your Gradle Home contains the source directory. If you use the wrapper this can be done by updating the distributionUrl to an -all version in the wrapper task.
You will also need to stop the Gradle daemons that are running otherwise they will keep their own "home": ./gradlew --stop, then you can go ahead and run the task: ./gradlew eclipse
See GRADLE-2133 (which this answer was adapted from)
I don't have an answer for eclipse but I can tell you how I do this for intellij which might give you some inspiration. It would be nice if this were available more easily.
private void addGradleSourceDeps() {
PathFactory pf = new PathFactory()
pf.addPathVariable('GRADLE_HOME', project.gradle.gradleHomeDir)
project.extensions.idea.module.iml.whenMerged { Module module ->
module.dependencies.grep {
it instanceof ModuleLibrary && ((ModuleLibrary) it).classes.grep { Path path ->
path.relPath.substring(path.relPath.lastIndexOf('/') + 1).startsWith('gradle-')
}
}.each { ModuleLibrary lib ->
// TODO this needs to be fixed for gradle 1.9 which now includes separate sub directory for each jar
// for now a workaround is to execute the following
// cd $GRADLE_HOME
// for each in $(find . -mindepth 2 -maxdepth 2 -type d ! -name META\-INF); do cp -a ${each} .;done
lib.sources.add(pf.path('file://$GRADLE_HOME$/src'))
}
module.dependencies.grep {
it instanceof ModuleLibrary && ((ModuleLibrary) it).classes.grep { Path path ->
path.relPath.substring(path.relPath.lastIndexOf('/') + 1).startsWith('groovy-all')
}
}.each { ModuleLibrary lib -> lib.sources.add(pf.path('file://$GROOVY_SRC_HOME$')) }
}
}
This relies on me having installed a gradle src distribution into a location available via the GRADLE_HOME path variable in intellij (and similar for GROOVY_SRC_HOME). You can also see my plugin currently uses gradle 1.8, the src layout changed in 1.9 so I need to fix this when I upgrade.

Eclipse how to reference file in a different src under the same project

My current setup in eclipse is like this:
trunk
--working/src
--resources
I have a java file inside a package under working/src and I am trying to retrieve a file in the resources. The path I am using is "../resources/file.txt". However I am getting an error saying that the file does not exist.
Any help would be appreciated thanks!
Considering your structure
I have package like
trunk
working/src/FileRead.java
resources/name list.txt
Following Code might solve your problem
package working.src;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class FileRead {
public FileRead() {
URL url = getClass().getResource("/resources/name list.txt");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String nameList;
while ((nameList = in.readLine()) != null) {
System.out.println(nameList);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new FileRead();
}
}
Files will be referenced relative to your project path, so use "resources/file.txt" to reference the file.
However, if you want the file to be accessible when you export the program as a JAR, the path "resources/file.txt" must exist relative to your JAR.
It depends on how you have specified your Java Build Path inside eclipse. I have tested two setups with different results:
Define the directory working/src only as build path. You can get the information what is in your build path through: Select project > Properties > Java Build Path > Tab Source. There are all source folders listed. You see there that there is a default output folder defined. All resources that are contained in a build path are copied to the default output folder, and are then available for the eclipse classes there. You can check that in the resource perspective in your bin folder if the resource is copied there. In this setup, only the classes are generated, resources are not copied (and therefore not found).
Define the directory working/src and resources as build path. Then the resource is copied and may then found by the path "file.txt"
So eclipse has a simple build process included and hides that from the developer, but it is a build process nonetheless.