# Building a libraries from a list

**URL:** <https://discuss.gradle.org/t/building-a-libraries-from-a-list/10607>\
**Category:** Help/Discuss\
**Created:** [July 14, 2015, 8:18pm UTC](https://discuss.gradle.org/t/building-a-libraries-from-a-list/10607 "2015-07-14T20:18:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterMitrano](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@PeterMitrano](https://discuss.gradle.org/u/PeterMitrano)\
**Post date:** [July 14, 2015, 8:18pm UTC](https://discuss.gradle.org/t/building-a-libraries-from-a-list/10607/1 "2015-07-14T20:18:42Z")

</div>

I have a bunch of C++ libraries that I want to build using gradle. They’re all plugins for Gazebo and all have the same exact link/compile setup. Previous, in CMake, I had a list that I iterated over to build each plugin. What is the gradle-way to do this?

[I’ve tried this](https://gist.github.com/PeterMitrano/3bfde6ef7e2154e7d306), but the syntax is wrong. I have no clue how groovy works, and even less about how Gradle works. I’m just doing this based on examples and intution.

Anyone know a clean solution to this?

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [July 14, 2015, 10:55pm UTC](https://discuss.gradle.org/t/building-a-libraries-from-a-list/10607/2 "2015-07-14T22:55:41Z")

</div>

I think you’re almost there. Try this:

```
"$it"(NativeLibrarySpec)
```

---

<div class="post-metadata">

**Author:** ![PeterMitrano](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@PeterMitrano](https://discuss.gradle.org/u/PeterMitrano)\
**Post date:** [July 15, 2015, 3:23pm UTC](https://discuss.gradle.org/t/building-a-libraries-from-a-list/10607/3 "2015-07-15T15:23:24Z")

</div>

Ok, it works. But what’s going on here?  
I’m only guessing here, but I think `model` and `components` are closures. is `“gz\_$it”(NativeLibrarySpec){} also some kind of closure, or is it more like a normal java function? How come I can give it a string as it’s name? Is NativeLibrarySpec a gradle defined constant, or what?

I think questions like these are why I don’t get gradle.

```gradle
//iterate over each plugin
model {
  components {
    plugins.each {
      "gz_$it"(NativeLibrarySpec){
        sources {
 

```

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [July 15, 2015, 3:42pm UTC](https://discuss.gradle.org/t/building-a-libraries-from-a-list/10607/4 "2015-07-15T15:42:07Z")

</div>

Essentially what is happening here is when you do something like `lib1(NativeLibrarySpec)` you are creating a new `NativeLibrarySpec` with the name ‘lib1’. Syntactically this is actually a method call. What you really want is for the name to be the _value_ of the current item you are iterating on, rather than the literal `it`. What was happening before is you were trying to create multiple components with the name ‘it’.

---

<div class="post-metadata">

**Author:** ![PeterMitrano](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@PeterMitrano](https://discuss.gradle.org/u/PeterMitrano)\
**Post date:** [July 15, 2015, 3:57pm UTC](https://discuss.gradle.org/t/building-a-libraries-from-a-list/10607/5 "2015-07-15T15:57:33Z")

</div>

So if I have a list of plugins [‘clock’, ‘motor’, ‘encoder’], then I am calling a methods:

```gradle
gz_clock(NativeLibrarySpec) 
gz_motor(NativeLibrarySpec) 
gz_encoder(NativeLibrarySpec)

```

But where are these functions defined?  
When you say I’m "creating a new NativeLibrarySpec " you don’t mean like an object of type NativeLibrarySpec? Isn’t NativeLibrarySpec a parameter to the methods I am creating?

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [July 15, 2015, 5:29pm UTC](https://discuss.gradle.org/t/building-a-libraries-from-a-list/10607/6 "2015-07-15T17:29:06Z")

</div>

> [@PeterMitrano](#):
>
> But where are these functions defined?

They aren’t. We interpret this at runtime as “create a new thing of type `NativeLibrarySpec` called 'gz\_clock`”.

If you’re curious about the implementation details, we use Groovy’s [method missing](http://www.groovy-lang.org/metaprogramming.html#_methodmissing) feature to make this work.

---

<div class="post-metadata">

**Author:** ![PeterMitrano](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@PeterMitrano](https://discuss.gradle.org/u/PeterMitrano)\
**Post date:** [July 15, 2015, 5:51pm UTC](https://discuss.gradle.org/t/building-a-libraries-from-a-list/10607/7 "2015-07-15T17:51:23Z")

</div>

I mean, I could ask why you don’t just do `new NativeLibrarySpec` and pass it the `gz_clock` and `sources` closure or whatever, but I have no idea what I’m talking about.

Thanks for answering my questions! I will definitely be posting more if I stick with this.

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [July 15, 2015, 5:54pm UTC](https://discuss.gradle.org/t/building-a-libraries-from-a-list/10607/8 "2015-07-15T17:54:29Z")

</div>

> [@PeterMitrano](#):
>
> I mean, I could ask why you don’t just do new NativeLibrarySpec and pass it the gz\_clock and sources closure or whatever, but I have no idea what I’m talking about.

Because it’s important that Gradle controls instantiation of these objects. In fact, `NativeLibrarySpec` is an interface, whose implementation is internal, or possibly, generated at runtime.
